doushu5451 2012-03-12 08:13
浏览 14
已采纳

有什么方法可以在(1,2)(2,1)友谊场景中找到共同的朋友?

I'm developing a website wherein when a person adds another person as a friend, 2 entries are created. Let's say uid 1 adds uid 2 as a friend, the following rows are created in MySQL.

activity_id   uid1 uid2
     1         1     2
     2         2     1

uid 2 becomes friends with uid 3:

activity_id   uid1 uid2
     1         1     2
     2         2     1
     3         2     3
     4         3     2

What are the ways to get the mutual friends between uid 1 and uid 2? I'm using php and MySQL with no PDO experience (for now).

[EDIT] Okay, so I decided to organize/normalize the tables. The relationship only generates 1 row now. which makes the table:

activity_id   uid1 uid2
     1         1     2
     2         2     3
  • 写回答

4条回答 默认 最新

  • douxi3432 2012-03-12 13:46
    关注

    Looking at the table format from your other question, this should do what you want;

    SELECT name FROM users u
    JOIN friends f1
      ON u.uid = f1.uid OR u.uid = f1.fid
    JOIN friends f2
      ON u.uid = f2.uid OR u.uid = f2.fid
    WHERE (f1.uid=1 OR f1.fid=1) AND (f2.uid=3 OR f2.fid=3) 
      AND u.uid<>1 AND u.uid<>3;
    

    Demo here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP