我有一个'用户'表
user_id|
1 |
2 |
3 |
4 |
5 |
和'阻止列表'表
blocker_id|blocked_id
1 | 3
2 | 3
现在我想搜索用户'中的用户表格未被该用户阻止 AND 该用户未被用户阻止!
(不是彼此阻挡) 像这样:
SELECT * FROM users
答案 0 :(得分:2)
尝试使用NOT IN()
:
SELECT *
FROM users
WHERE user_id NOT IN (SELECT blocked_id FROM blocklist)
答案 1 :(得分:0)
以下是答案:
SELECT * FROM users WHERE user_id NOT IN (SELECT blocked_id FROM blocklist WHERE blocked_id = users.user_id AND blocker_id = X ) AND user_id NOT IN (SELECT blocker_id FROM blocklist WHERE blocker_id = users.user_id AND blocked_id = X )
X是我想要不被sql结果阻止或阻止的user_id