2

我有 2 张桌子:

articles:
id
title
text
timestamp

articles_views:
id
article_id
timestamp

我需要 sql 查询,该查询将返回按该文章的articles_views 计数排序的所有文章的列表。

4

2 回答 2

5
SELECT
    a.id
    , a.Title
    , COUNT(v.id)
FROM
    articles a
LEFT JOIN
    article_views v
ON a.id = v.article_id
GROUP BY
    a.id, a.title
ORDER BY
    COUNT(v.id) DESC
于 2012-12-14T16:48:18.860 回答
1

这将解决您的问题-

SELECT articles.*, 
(select count(*) from `articles_views` C where P.id = C.article_id ) as CNT 
FROM articles P inner join articles_views C 
  ON P.id = C.article_id group by P.id
order by cnt desc
于 2012-12-14T16:55:59.550 回答