【发布时间】:2012-12-08 13:23:05
【问题描述】:
数据库:
表 #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 好友之一匹配的所有问题,但它不会选择任何内容。知道有什么问题吗?
编辑
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
【问题讨论】:
-
您的 ON 语句似乎在限制它。 WHERE 甚至没有机会找到其他匹配项,因为 on 语句限制了 user_id。
-
ON 语句不应该限制任何东西...我确定问题 stable 中的 user_id 是用户表的外键。
-
你能告诉我们实际的 SQL 是什么样子而不是它的 PHP 版本吗?
-
我该怎么做?
-
只打印 $sql 变量:
print $sql;
标签: php mysql sql database join