简体   繁体   中英

How can I select multiple rows from a table using a range of values in WHERE clause?

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?

SELECT *
FROM `Commenting Table`
WHERE member_id=10
OR member id IN (
  SELECT friend_id FROM `Friends Table` WHERE member_id=10
-- Uncomment the following if your Friends Table is asymetrical
--  UNION
--  SELECT member_id FROM `Friends Table` WHERE friend_id=10
)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM