当前位置: 代码迷 >> ASP.NET >> 论坛主题贴按回贴数从大到小排序,SQL语句如何写
  详细解决方案

论坛主题贴按回贴数从大到小排序,SQL语句如何写

热度:9001   发布时间:2013-02-26 00:00:00.0
论坛主题贴按回贴数从大到小排序,SQL语句怎么写?

主题贴表是bbs
回贴表是huifu

它们之间关联的是bbs_id字段

bbs表的字段有
bbs_id       -----------主题贴ID
bbs_name   ----------主题贴
bbs_nr     -----------内容

而huifu表字段有
hf_id   ---------回复贴ID
hf_nr     --------------内容
bbs_id     -------------主题贴ID
=================================================
论坛主题贴按回贴数从大到小排序,SQL语句怎么写?

------解决方案--------------------------------------------------------
select a.* from bbs inner join huihu as bon a.bbs_id = b.bbs_id group by a.*order by count(*) desc
------解决方案--------------------------------------------------------
create database bbs
create table bbs
(
bbs_id int,
bbs_name nvarchar(20),
bbs_nr nvarchar(50)
)
create table huifu
(
hf_id int,
hf_nr nvarchar(50),
bbs_id int
)
insert into bbs values(1, 'a ', 'aa ')
insert into bbs values(2, 'b ', 'bb ')
insert into bbs values(3, 'c ', 'cc ')

insert into huifu values(1, 'a1 ',1)
insert into huifu values(2, 'b1 ',2)
insert into huifu values(3, 'b2 ',2)
insert into huifu values(4, 'c1 ',3)
insert into huifu values(5, 'c2 ',3)
insert into huifu values(6, 'c3 ',3)


select * from bbs order by (select count(*) from huifu where bbs.bbs_id = huifu.bbs_id ) desc


drop table bbs,huifu
  相关解决方案