我在为我的网站制作共同朋友代码时遇到了困难。我在这里阅读了一些帖子,但没有帮助。
这是我的mysql查询:
select *
from friend_list
where uid='7'
and status=1
and friend_id !='3'
union
select *
from friend_list
where uid='3'
and status=1
and friend_id !='7'
这将显示登录用户的所有朋友以及我访问的个人资料的朋友。 输出就是这个----
id uid friend_id status
36 7 4 1
39 7 5 1
40 7 8 1
1 3 4 1
从这个表中我只想要freind_id 4,因为这是相互的。
答案 0 :(得分:1)
SELECT *
FROM friend_list AS f
INNER JOIN friend_list AS mf ON f.friend_id = mf.friend_id
WHERE f.uid = 7
AND f.status = 1
AND mf.uid = 3
AND mf.status = 1
答案 1 :(得分:0)
我刚刚开始学习MySQL,我已经解决了这个问题。
personID friendID
6 10
6 2
6 3
8 1
8 2
8 3
/* query for friends */
select f.personID, p.personID, firstName, lastName
from person p
inner join friends f on f.friendID = p.personID
where f.personID = 6;
/* query for common friends */
select f1.personID 'personID 1', f2.personID 'personID 2', f1.friendID 'common friend'
from person p
inner join friends f1 on f1.friendID = p.personID
inner join friends f2 on f2.friendID = p.personID
where f1.personID = 6 and f2.personID = 8 and f1.friendID = f2.friendID;
结果:
personID 1 personID 2 common friend
6 8 2
6 8 3
虽然它不是您代码的直接答案,但您可以将我的代码视为来源。