【发布时间】:2015-05-18 19:23:47
【问题描述】:
我有两张桌子
一个是 user_matchs
id | user_id | matched_user_id | status
36 | 17 | 24 | passed
37 | 17 | 25 | friend
另一个是用户
id | name | address | age
17 | mamun | test | 23
25 | shihab | test2 | 30
24 | shihab2 | test22 | 30
现在我想根据给定userId的某些条件检索所有用户的列表
假设我是 17 号用户。现在我想查看所有不是我朋友并且通过我的用户。如果 user_matchs 表
user_id=17 and matched_user_id=24 and status=passed 那么这意味着我通过了用户 24 但 24 没有通过我如果 24 也通过了我那么行将是
user_id=17 and matched_user_id=24 and status=friend
另一种逻辑
user_id=24 and matched_user_id=17 and status=passed 那么这意味着用户 24 通过了我但我没有通过用户 17 如果 17 也通过了我那么行将是
user_id=24 and matched_user_id=17 and and status=friend
当我给 userId 17 提供搜索查询时,它不会返回任何用户,因为用户 25 是用户 17 的朋友,并且用户 17 已经通过了用户 24。
但是当我在搜索查询中给出 userId 24 时,它会返回用户 17 和 25。因为用户 25 不是朋友或未通过用户 24,而用户 17 不是朋友或用户 24 未通过用户 17。
我正在尝试使用此查询,但它不能正常工作:这里给定 24 id
SELECT *
FROM `users`
WHERE users.id != 24 AND
users.id NOT IN (SELECT matched_user_id
FROM `user_matchs`
WHERE user_id = '24'
)
and
users.id NOT IN (SELECT user_id
FROM `user_matchs`
WHERE matched_user_id = '24'
)
【问题讨论】:
-
查询应该做什么? “给定
userId的某些条件”是什么意思? -
我更新了我的问题,请看@Barmar
-
您的查询从不检查
status列,它应该如何判断用户是否通过了您? -
我现在该如何解决。你能给我任何查询逻辑@Barmar
-
你的 sqlfiddle 只返回 25。你为什么说它返回 17 和 25?这是你想要它返回的吗?