求一个sql语句的编写

来源:百度知道 编辑:UC知道 时间:2024/09/22 04:37:09
假定有两个表:
news(newsID,title,content,time);
comment(commentID,content,time,newsID);

如果要查询评论最多的前十条新闻的 newsID,title 及每条新闻的评论数该怎么写?

--oracle中,改了下,comment好像是关键字,换了个名字
select x.newsID,x.title,z.评论数
from
news x,
(
select * from
(select a.newsID,count(a.commentID) as 评论数
from
newcomment a
group by a.newsID
order by count(a.commentID) desc
)y
where rownum<=10
)z
where x.newsID = z.newsID
order by z.评论数 desc

--其他,取前10条记录的方式不同
select x.newsID,x.title,z.评论数
from
news x,
(select top10 a.newsID,count(a.commentID) as 评论数
from
newcomment a
group by a.newsID
order by count(a.commentID) desc
)z
where x.newsID = z.newsID
order by z.评论数 desc