dregduc63699 2011-01-12 21:00
浏览 246
已采纳

在mysql中设置好友列表

I want to make a friends list in my online game. I am not sure how to set it up or where to start. The add friends and accept friends part I can handle, but I don't know how to set it up in mysql and php. A list of users (friends) connected to each user or something?

  • 写回答

4条回答 默认 最新

  • dsgixl5195 2011-01-12 21:02
    关注

    If your friendship relationship is symmetrical, you can either store each pair in a separate record:

    friend1 friend2
    A       B
    B       A
    A       C
    B       D
    C       B
    D       B
    

    and query all B's friends like that:

    SELECT  friend2
    FROM    friends
    WHERE   friend1 = 'B'
    

    or store the user with the least id in the first field and that with the greatest id in the second one:

    friend1  friend2
    A        B
    A        C
    B        D
    

    and query B's friends like that:

    SELECT  friend1
    FROM    friends
    WHERE   friend2 = 'B'
    UNION ALL
    SELECT  friend2
    FROM    friends
    WHERE   friend1 = 'B'
    

    The first option is a little bit more efficient in MySQL, and this is the only option if your friendship relationship is not symmetrical (like on LiveJournal)

    See this article:

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大