[英]How to find people who likes each other?
我正在使用 mysql。 用戶有 likes_table。 但是,我堅持要找到彼此喜歡的人。
likes_table:
id (unique), from_user_id, to_user_id
1,3,1
2,1,3
3,1,64
4,1,2
5,2,1
例如,我想找到 from_user_id =1 的相互喜歡因此預期結果應該是:
2,3
我試圖用 IN 命令連接兩列。
我試過的 SQL 代碼:
Select to_user_id from likes_table where from_user_id IN (Select from_user_id from likes_table where to_user_id = #id)
這僅給出 user_id=1
一種方法是根據兩個條件過濾表:
from_user_id = 1
; 和, 可以使用exists
處理第二個條件:
select l.to_user_id
from likes l
where l.from_user_id = 1 and
exists (select 1
from likes l2
where l2.from_user_id = l.to_user_id and
l2.to_user_id = l.from_user_id
)
你快到了,你想要的實際上是 from_id :
Select from_user_id from likes_table where from_user_id IN (Select from_user_id from likes_table where to_user_id = #id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.