2

数据库:

表 #1: xx_users (id, name, user_id, location, network)

表 #2: xx_questions (id, user_id, time)

代码:

$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
4

1 回答 1

3

正确的代码是:

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
于 2012-12-23T13:35:59.480 回答