gpt4 book ai didi

php - 一起使用 JOIN、ON 和 WHERE 的 MySQL 语句不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:20 24 4
gpt4 key购买 nike

数据库:

表 #1:xx_users(id、名称、user_id、位置、网络)

表 #2:xx_questions(id、user_id、时间)

代码:

$friends = $facebook->api('/me/friends');
$arr= $friends['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
$friend_ids_arr[] = $friend['id'];
}

$sql = "SELECT q.*, u.location, u.network
FROM xx_questions q
JOIN xx_users u
ON q.user_id = u.user_id
WHERE q.user_id = $user OR
//*** q.user_id IN (".implode(',', $friend_ids_arr).") OR
u.location = (SELECT location FROM xx_users WHERE user_id = $user) OR
u.network = (SELECT network FROM xx_users WHERE user_id = $user)
ORDER BY q.time DESC";

问题行是用星号注释掉的行。它应该选择提问者的 ID 与用户的一位 Facebook 好友的 ID 相匹配的所有问题,但它没有选择任何内容。知道出了什么问题吗?

编辑

SQL 输出为:

SELECT q.*, u.location, u.network
FROM xx_questions q JOIN xx_users u
ON q.user_id = u.user_id
WHERE q.user_id = 528782060 OR q.user_id IN (123456, 234567 etc) OR
u.location = (SELECT location FROM xx_users WHERE user_id = 528782060) OR
u.network = (SELECT network FROM xx_users WHERE user_id = 528782060)
ORDER BY q.time DESC

最佳答案

正确的代码是:

SELECT q.*, u.location, u.network
FROM xx_questions q JOIN xx_users u
ON q.user_id = u.user_id
AND q.user_id = 528782060 OR q.user_id IN (123456, 234567 etc) OR
u.location = (SELECT location FROM xx_users WHERE user_id = 528782060) OR
u.network = (SELECT network FROM xx_users WHERE user_id = 528782060)
ORDER BY q.time DESC

关于php - 一起使用 JOIN、ON 和 WHERE 的 MySQL 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com