嗨,我有这两个表:用户和朋友(friend_status = 1 表示请求已发送,friend_status = 2 表示他们是朋友)。现在我想选择所有用户都不是任何人的朋友。怎么做?
假设当前用户是 1。我试过这个,但它似乎是错误的。因为它只显示发送请求但尚未确认的用户。
SELECT user_id, name, email
FROM
(
SELECT user_id, name, email
FROM users u INNER JOIN friends f ON u.user_id = f.sender
WHERE f.receiver = 1 AND friend_status <> 2
UNION
SELECT user_id, name, email
FROM users u INNER JOIN friends f ON u.user_id = f.receiver
WHERE f.sender = 1 AND friend_status <> 2
) T
LIMIT 0, 10