2016-03-04 23 views
0

全新的子查詢,我試圖從表showcase通過他們的評論數(按降序)並不知道如何訂購帖子。按次數(*)子查詢

SELECT * 
FROM showcase 
ORDER BY 
    (select count(*) 
    from comments) 
DESC 
+0

您的查詢中展示和評論之間沒有關係。 –

+1

更好得到表structute –

+0

評論具有'item_id',這是'id'在陳列櫃 – frosty

回答

0

我們需要沿着使用組數:

select s.postId, count(c.commentid) as comments 
from showcase s join comment c on s.postId = c.postId 
group by s.postId 
order by comments desc 
1

如果你只需要身份證,group by和每個組

select showcase.id 
from showcase left join comments on comments.item_id = showcase.id 
group by showcase.id 
order by count(showcase.id) desc 
0

,你可以指望行嘗試一個更適合查詢的連接查詢。

SELECT `showcase`.*, count(`comments`.`item_id`) as item_id FROM `showcase` 
LEFT JOIN `comments` 
ON `showcase`.`id`, `comments`.`item_id` 
ORDER BY item_id DESC 

讓我知道是工作或沒有。