[英]How can I select multiple rows from a table using a range of values in WHERE clause?
這個問題是如何使用WHERE子句中的一系列值從表中選擇多行。 這是我想要做的。
我有一個評論應用程序,您可以在其中發布評論,您的朋友可以回復評論,有點像facebook的牆。 在同一個應用程序中,我允許每個成員彼此成為朋友。
我現在面臨的挑戰是,我不僅要顯示所有者的評論,而且還要顯示其朋友。 我想要一個與Facebook牆類似的功能。
所以這是我所擁有的:
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 |
------------------------ --------------------------------------------
因此,成員10是成員14和17的朋友。我不僅要顯示成員10的評論,還希望顯示成員10、14和17的評論。像這樣:
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 |
--------------------------------------------
所以我正在嘗試mysql_query(“ SELECT comment Commenting Table
WHERE member_id
='10'ORDER BY member_id DESC”)並執行一堆while循環,但這並不是很有效。 還有其他使用多個值選擇注釋的方法嗎?
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
)
您可以按朋友的ID和成員的ID加入表格,然后按成員的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.