dsilhx5830 2012-08-01 22:33
浏览 40
已采纳

编程好友列表功能

I'd like to firstly point that I'm not very good at advanced MySQL just yet. So please forgive me. What I am trying to do is create a friends list like what's in the image below:

enter image description here

Here is my db structure:

friends table: enter image description here

users table: enter image description here

Here is my code so far:

<?php  $query3 = $this->db->query("SELECT * FROM friends WHERE node1id = '{$myuserid}'");
        foreach($query3->result() as $row1) {
        echo $row1->node1id."<br>"; } ?>

I know this code isn't logically correct. What I am trying to do is pull in the users table. And if relationType = friends, display firstname and lastname of the user in the friends list. I have two variables. $selectedId and $myuserid. $selectedId is the id of the profile the user is viewing. and $myuserid is the id of the logged in user. How would I code this type of feature logically?

  • 写回答

3条回答 默认 最新

  • dongzhong1929 2012-08-01 22:40
    关注
    SELECT username FROM users 
    WHERE userid IN 
        (SELECT node2id FROM friends WHERE node1id = '{$myuserid}' 
        AND relationType = 'friends')
    

    and

    echo $row1->username
    

    For mutual friends you could do this

    EDIT: Just realised last version of this wasn't right, fixed now (untested)

    SELECT username FROM users 
    WHERE userid IN 
        (SELECT node2id FROM (
            (SELECT node2id FROM friends WHERE relationType = 'friends' AND node1id <> '{$myuserid}' AND node1id IN
                (SELECT node2id FROM friends WHERE relationType = 'friends' AND node1id = '{$myuserid}'))) 
             WHERE node2id IN (SELECT node2id FROM friends WHERE relationType = 'friends' AND node1id = '{$myuserid}'))
    

    This first selects friends, then it selects the friends of those friends which is not equal (not equal is <>) to yourself. It then only selects from that list the friendIDs which match your your own friend IDs. It then selects the name of these users from the users table.

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

报告相同问题?

悬赏问题

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