表dating_users
:
user_id | username
---------+----------
11 | taimoor
13 | jacob
14 | pi
(3 rows)
表dating_likes
:
like_id | user_id | liked_user
---------+---------+------------
54 | 11 | 13
56 | 13 | 11
57 | 11 | 14
(3 rows)
我想在用户喜欢某人的地方找到喜欢的人,并且有人喜欢他们。这是我的查询:
select like_id from dating_likes where user_id = 11 and exists(select like_id from dating_likes where liked_user = 11);
结果:
like_id
---------
54
57
(2 rows)
这应该只导致第一行,因为54
is a like where theuser_id = 11
和 there is a like 56
where liked_user = 11
。Like57
不应该出现,因为它不是相互的 - 真实的user_id
是 11。