douluogu8713 2012-06-12 19:04
浏览 20
已采纳

如何使用WHERE子句中的一系列值从表中选择多行?

This question is how to go about selecting multiple rows from a table using a range of values in the WHERE clause. Here is what I am trying to do.

I have a commenting application in which you post a comment and your friends can respond to it, sort of like facebook's wall. And in the same application I allow each member to be friends with one another.

My challenge now is that instead of just showing the owner's comments, I want to show their friends as well. I want a similar functionality as facebook's wall.
So here is what I have:

    Friends Table                           Commenting Table
 _______________________        ___________________________________________
| member_id | friend_id |      | member_id |           comment             |
 -----------------------        -------------------------------------------
|    10     |    14     |      |    10     |      Hello member # 14        |
|    14     |    10     |      |    14     |      hey how are you          |
|    17     |    9      |      |    17     |      Hello world              |
|    17     |    10     |      |    10     |      Hello                    |
 ------------------------       --------------------------------------------

so member 10 is friends with member 14 and 17. And instead of just showing the comments for member 10, I want to show the comments of member 10, 14, and 17. Like this:

        Member Comments                          All Member Comments
 _______________________________        ___________________________________________
| member_id |      comment      |      | member_id |           comment             |
 -------------------------------        -------------------------------------------
|    10     | Hello member # 14 |      |    10     |      Hello member #           |
|    10     |       Hello       |      |    14     |      hey how are you          |
---------------------------------      |    17     |      Hello world              |
                                       |    10     |      Hello                    |
                                       --------------------------------------------

So i was trying mysql_query("SELECT comments Commenting Table WHERE member_id = '10' ORDER BY member_id DESC") and do a bunch of while loops but thats not very efficient. Is there any other way of selecting the comments using multiple values?

  • 写回答

2条回答 默认 最新

  • drduh44480 2012-06-12 19:11
    关注

    You can join the tables by the friend's id and the member's id and afterwards group by the member id:

    SELECT f.member_id AS user, c.member_id AS commentBy, c.comment AS comment
    FROM commentingTable AS c, friendsTable AS f
    WHERE c.member_id = f.member_id 
      OR c.member_id = f.friend_id
    GROUP BY f.member_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题