1,新聞表news
id 新聞編號
content 新聞內容
title 新聞標題
2,評論表。comment
id 評論編號
content 評論內容
newsId 新聞編號
要求:
使用MySQL查詢評論數量最多的10條新聞
uj5u.com熱心網友回復:
select top 10 a.id,a.content ,a.title ,b.com_qty from news a,(select count(1) as com_qty ,comment.newsId from comment group by comment.newsId ) b where a.newsId = b.newsId order by b.com_qty descuj5u.com熱心網友回復:
不行啊,報錯,,,內個b.com_qty是什么意思啊uj5u.com熱心網友回復:
select *from
(select a.id,count(b.id) as comment_cnt
from
news as a
inner join
comment as b on a.id = b.newsId
) as a
order by comment_cnt desc
limit 10
這樣試試
uj5u.com熱心網友回復:
select *
from
(select a.id,count(b.id) as comment_cnt
from
news as a
inner join
comment as b on a.id = b.newsId
group by a.id
) as a
order by comment_cnt desc
limit 10
少打了group by
uj5u.com熱心網友回復:
select a.id, count(b.id) as content_numfrom new as a
inner join content as b on (a.id = b.newsld)
group by a.id
order by count(b.id) desc
limit 10
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/46281.html
標籤:疑難問題
上一篇:Mysql查詢
下一篇:關于行鎖的問題
