我有一个包含三列id的表,follower_id,following_id,我只有一个 101 或 105 的ID现在我想要记录{{1}跟随user1
和user2
关注一个查询中的user2
。
表格结构
user1
结果应为1 - > 101-> 105或3-> 105-> 101
答案 0 :(得分:3)
如果您想要跟随者和跟随匹配的行,您可以使用自我加入
select t1.* from my_table as t1
inner join my_table as t2
where t1.follower_id = t2.following_id
and t1.following_id = t2.follower_id
答案 1 :(得分:0)
您可以使用least
和greatest
来执行此操作。
select * from t
where (least(follower_id,following_id),greatest(follower_id,following_id))
in (select least(follower_id,following_id),greatest(follower_id,following_id)
from t
group by least(follower_id,following_id),greatest(follower_id,following_id)
having count(*) > 1)
答案 2 :(得分:0)
可能是这个查询会帮助你......
SELECT *
FROM Yourtable A
WHERE EXISTS (SELECT 1 FROM Yourtable B WHERE B.follower = A.following)
AND EXISTS (SELECT 1 FROM Yourtable C WHERE C.following = A.follower)