【发布时间】:2013-01-25 16:39:04
【问题描述】:
我有一个简单的文章和 cmets 系统,包含以下表格:
文章表:
ID | Writer | Text
1 | Bob | good article
2 | Marley | read this
评论表:
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
预期结果:
Article 1: good article
Comments: 1 - i love this article
2 - good one
Article 2: read this
Comments: 3 - waiting for more
我得到了什么:
Article 1: good article
Comments: 2 - good one
Article 2: read this
Comments: 3 - waiting for more
那么我如何选择每篇文章及其 cmets,并按 id 降序排列文章,并按 ids 降序排列 cmets?
谢谢
【问题讨论】:
-
你的查询很好,它应该给你想要的结果,所以问题可能出在 php 代码上
-
它将显示每条评论的文章...所以文章 1 将显示两次,因为它有 2 个 cmets 等等...谢谢 :)
标签: php mysql sql database join