A表字段有 :                        B表字段有有                       id title content                   id belongid title content
1    1      1                       1  1        1      1
2    1      1                       2  1        1       1
3    1      1                       3  2        1       1上面的意思就是 A表中ID为1的有2个子类,id为2的有1个,其中b表是A表的子类,就是说belongid就是a表的id,我想在A来排序,,是根据A表的子类多排前面

解决方案 »

  1.   

    select *
    from a
    order by (select count(*) from b where b.belongid=a.id)
      

  2.   


    改了一下, 改成2的子类更多, 2排在前面-- =============================================
    -- Author:      T.O.P
    -- Create date: 20091202
    -- Version:     SQL SERVER 2000
    -- =============================================
    declare @tb1 table([id] int,[title] int,[content] int)
    insert @tb1
    select 1,1,1 union all
    select 2,1,1 union all
    select 3,1,1declare @tb2 table([id] int,[belongid] int,[title] int,[content] int)
    insert @tb2
    select 1,1,1,1 union all
    select 2,2,1,1 union all
    select 3,2,1,1select id, title, content
    from (
    select a.id, a.title, a.content, count(b.belongid) as icount
    from @tb1 a left join @tb2 b on a.id= b.belongid
    group by a.id, a.title, a.content
    ) t
    order by t.icount desc--测试结果:
    /*
    id          title       content     
    ----------- ----------- ----------- 
    2           1           1
    1           1           1
    3           1           1(所影响的行数为 3 行)警告: 聚合或其它 SET 操作消除了空值。
    */
      

  3.   

    select *
    from @tb1 a
    order by (select count(*) from @tb2 t where t.belongid=a.id)
    desc/*
    id          title       content                 
    ----------- ----------- ----------- ----------- 
    2           1           1           2
    1           1           1           1
    3           1           1           0(所影响的行数为 3 行)*/
      

  4.   

    declare @tb1 table([id] int,[title] int,[content] int)
    insert @tb1
    select 1,1,1 union all
    select 2,1,1 union all
    select 3,1,1declare @tb2 table([id] int,[belongid] int,[title] int,[content] int)
    insert @tb2
    select 1,1,1,1 union all
    select 2,2,1,1 union all
    select 3,2,1,1select m.* from @tb1 m left join (select belongid , count(1) cnt from @tb2 group by belongid) n
    on m.id = n.belongid order by n.cnt desc/*
    id          title       content     
    ----------- ----------- ----------- 
    2           1           1
    1           1           1
    3           1           1(所影响的行数为 3 行)
    */
      

  5.   

    --> 测试数据: @ta
    declare @ta table (id int,title int,content int)
    insert into @ta
    select 1,1,1 union all
    select 2,1,1 union all
    select 3,1,1
    --> 测试数据: @tb
    declare @tb table (id int,belongid int,title int,content int)
    insert into @tb
    select 1,1,1,1 union all
    select 2,1,1,1 union all
    select 3,2,1,1 select t1.* from @ta t1,(
    select a.id,px=sum(case when b.belongid is null then 0 else 1 end ) from @ta a left join @tb b
    on a.id=b.belongid
    group by a.id
    )t2
    where t1.id=t2.id
    order by px desc(3 行受影响)
    id          title       content
    ----------- ----------- -----------
    1           1           1
    2           1           1
    3           1           1(3 行受影响)