gpt4 book ai didi

mysql - 如何在mysql中查找 friend 的 friend

转载 作者:行者123 更新时间:2023-11-29 07:48:55 25 4
gpt4 key购买 nike

描述:

我有一个用户表如下

学生

id ------ name     
1 ------ John
2 ------ Sarah
3 ------ Peter

和 friend 表为

好友

person1 ------  person2    
1 ------ 2
2 ------ 3

现在我想要 2 的所有 friend 以及我的 friend 不是我 friend 的所有其他 friend 。

例如,在“您可能认识的人”中,我们看到的人是我们 friend 的 friend ,但不是我们的 friend

我已经成功编写了查找我所有 friend 的查询,但我不确定如何在一个查询中查找“我 friend 的 friend ”

有没有办法在一个查询中完成它......

我正在寻找这样的 friend

 select * from `students` join `buddy_circle` on 
'$reg_no' = `person_1` and `stregno` = `person_2` or
'$reg_no' = `person_2` and `stregno` = `person_1`

其中 stregno 是学生的 ID,buddy_circle 是 friend 表,$regno 是用户的 ID

最佳答案

也许是这个?我仅使用您的示例数据对其进行了测试。

select name from students where id in (
select p2 from buddies where p1 in (
select p2 from buddies where p1=[serach_for_id]));

关于mysql - 如何在mysql中查找 friend 的 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com