sql语句 实现两个表查询显示,有一个聚合函数count()
我有一个news表和一个comment表,我想查询news的所有内容和每条new的评论总数,该怎么写?我是用newid连接两个表的"selecttop10n.newId,...
我有一个news表和一个comment表,我想查询news的所有内容和每条new的评论总数,该怎么写?我是用newid连接两个表的
"select top 10 n.newId,count(c.*) from new as n join comment as c on c.newId=n.newId where n.newId=@id group by n.newId order by count(c.*) desc";
这样貌似不对 展开
"select top 10 n.newId,count(c.*) from new as n join comment as c on c.newId=n.newId where n.newId=@id group by n.newId order by count(c.*) desc";
这样貌似不对 展开
5个回答
展开全部
这个我们首先可以对2张表按newsid进行连接,
然后按new进行分组计数即可。
select n.newId, count(c.*)
from new as n
left join comment as c on c.newId = n.newId
group by n.newId
然后按new进行分组计数即可。
select n.newId, count(c.*)
from new as n
left join comment as c on c.newId = n.newId
group by n.newId
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select top 10 n.*,d.p from new n, (select count(*) as p,newId from comment group by newId) d where n.newId=@id and n.newId = d.newId order by d.p desc
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select n.newId, count(c.*)
from new as n
left join comment as c on c.newId = n.newId
group by n.newId
order by count(c.*) desc
应该没问题的。
from new as n
left join comment as c on c.newId = n.newId
group by n.newId
order by count(c.*) desc
应该没问题的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select
News.*
,C.Num
from
News
AS N
inner
join
(
select
NewID,count(*)
AS
Num
from
comment group
by
NewID
)
AS
C
on
N.ID=C.NewIDorder
by
C.Num
DESC
News.*
,C.Num
from
News
AS N
inner
join
(
select
NewID,count(*)
AS
Num
from
comment group
by
NewID
)
AS
C
on
N.ID=C.NewIDorder
by
C.Num
DESC
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询