【发布时间】:2013-01-25 21:05:54
【问题描述】:
我有一个简单的文章和 cmets 系统,包含以下表格:
文章表:
id | writer | text
1 | Bob | first article
2 | Marley | second article
评论表:
id | article_id | comment
1 | 1 | i love this article
2 | 1 | good one
3 | 2 | waiting for more
我想选择每篇文章及其下方的 cmets。我使用以下查询:
SELECT * FROM articles LEFT JOIN comments ON articles.id = comments.article_id
我得到的结果:
articles.id | articles.writer | articles.text | comments.id | comments.article_id | comments.comment
1 | Bob | first article | 1 | 1 | i love this article
1 | Bob | first article | 2 | 1 | good one
2 | Marley | second article | 3 | 2 | waiting for more
我想要什么:
articles.id | articles.writer | articles.text | comments.id | comments.article_id | comments.comment
1 | Bob | first article | 1 | 1 | i love this article
NULL | NULL | NULL | 2 | 1 | good one
2 | Marley | second article | 3 | 2 | waiting for more
那么我如何选择每篇文章及其 cmets 并且只显示一次文章而不是每条评论
谢谢
【问题讨论】:
-
在加入时,您正在组合结果。如果您不想重复,我认为您需要 2 个查询。
-
我认为应该有其他方法...在 php while 循环中查询也会停止第一个查询,所以我认为这也行不通...谢谢 :)
标签: php mysql sql database join