提问者:小点点

SQL左联接问题(所有行未来)


表列如下:

Users表(表名:Users):

[用户表]

Friends表(表名:Friends):

“好友表”

我的Sql代码:

SELECT
    friend_id,
    friend_adding_id,
    friend_added_id,
    user_id,
    user_name
FROM friends
LEFT JOIN users ON friends.friend_added_id = users.user_id
WHERE
    friend_adding_id = 1
    OR friend_added_id = 1
    AND friend_confirm = 1
ORDER BY friends.friend_id DESC
LIMIT 1, 10

留下下面一栏:

“离开栏”

如何修复此缺失的查询?请帮帮我.


共1个答案

匿名用户

首先,您缺少了一行而不是列或者不是查询,其次,这是因为与/或优先级。并且具有比或更高的优先级,要使逻辑正常工作,必须通过使用括号给予或更高的优先级:

SELECT
    friend_id,
    friend_adding_id,
    friend_added_id,
    user_id,
    user_name
FROM friends
LEFT JOIN users ON friends.friend_added_id = users.user_id
WHERE
    (friend_adding_id = 1 OR friend_added_id = 1) -- < like this
    AND friend_confirm = 1
ORDER BY friends.friend_id DESC
LIMIT 1, 10