SQL搜索用户不在该用户的黑名单中

时间:2017-06-16 19:28:14

标签: sql

我有一个'用户'表

user_id|
1      | 
2      | 
3      | 
4      | 
5      | 

和'阻止列表'表

blocker_id|blocked_id
1         | 3
2         | 3

现在我想搜索用户'中的用户表格未被该用户阻止 AND 该用户未被用户阻止!

(不是彼此阻挡) 像这样:

SELECT * FROM users 

2 个答案:

答案 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