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条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题