MySQL - 从结果中排除所有被阻止的用户

标签 mysql sql


我对 MySQL 查询有点困惑:

我有表用户

user_id|language|name
1      |en      |Mike
2      |en      |John
3      |en      |Jesse
4      |de      |Wolfgang (Me)

和表users_blocked

user_id|blocked_user_id
1      |4
4      |1
1      |3
3      |1
4      |2

(其中 user_id 已阻止 blocked_user_id)

我想找到所有说英语 (en) 的用户(不包括我自己),我没有屏蔽他们,他们没有屏蔽我。 它应该只返回上面示例中 ID 为 3 的用户。

我正在构建一个语言合作伙伴搜索,它从结果中排除所有被阻止的用户(双向)。

最佳答案

来,试一试:

select * from users where user_id <> 4 and language = 'en'
    and user_id not in(select blocked_user_id from users_blocked where user_id = 4)
    and user_id not in(select user_id from users_blocked where blocked_user_id = 4)

此处演示:http://sqlfiddle.com/#!2/b22a4/2

关于MySQL - 从结果中排除所有被阻止的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19345689/

相关文章:

mysql - 在加入冗余和得到

sql - 通过 Powershell 连接到 SQL Server(非本地)

mysql - SQL:Group By 记录不匹配

mysql - sql创建表,条件是2列不能具有相同的值

c# - Linq Any() 抛出错误 : String was not recognized as a valid boolean

php - MYSQL-PHP : Display products page issue

sql - 仍然无法使用 JOIN 在 SQL 中删除

mysql - 根据每天的最大值组连接表

mysql - 获得最高分并在您之后的下一个和上一个得分 3

php - 插入后获取id并将其作为值保存到另一个表中