求一条Sql语句 在线等~~~~~~~~~~~~
A表放的是文章
B表放的是评论,B标中只有文章的ID
如何按照评论的多少对文章进行排序?
------解决方案--------------------select ( .....,id , (select count(*) from B where (B.ID=A.ID)) as "count " ) FROM A order by count desc
------解决方案---------------------- 初始化表 赋值
Declare @A table( TID int, Article nvarchar(4000) )
Declare @B table( PID int, TID int, PContent nvarchar(1000) )
insert into @A
select 1, 'aa ' union
select 2, 'bb ' union
select 3, 'cc ' union
select 4, 'dd '
insert into @B
select 1, 2, 'AA ' union
select 2, 2, 'BB ' union
select 3, 1, 'CC ' union
select 4, 3, 'DD ' union
select 5, 3, 'EE ' union
select 6, 2, 'FF ' union
select 7, 1, 'GG ' union
select 8, 2, 'HH '
--主语句
select A.TID, Article, count(PID) as AAA
from @A A
inner join @B B
on A.TID = B.TID
group by A.TID, Article
order by AAA desc
------解决方案--------------------select A.*,B.* from A inner join B on A.id=B.id group by B.id order by...