create table tb(id int,name char)
insert into tb
select 1,'A' union all
select 6,'C' union all
select 5,'B' union all
select 2,'A' union all
select 7,'C' union all
select 3,'A' union all
select 8,'D' union all
select 4,'B'
select *,num=(select count(1) from tb where name=a.name) 
from tb a  order by num desc
drop table tb

解决方案 »

  1.   

    --result
    id          name num         
    ----------- ---- ----------- 
    1           A    3
    2           A    3
    3           A    3
    7           C    2
    4           B    2
    5           B    2
    6           C    2
    8           D    1(所影响的行数为 8 行)
      

  2.   

    create table tb(id int,name char)
    insert into tb
    select 1,'A' union all
    select 6,'C' union all
    select 5,'B' union all
    select 2,'A' union all
    select 7,'C' union all
    select 3,'A' union all
    select 8,'D' union all
    select 4,'B'
    select *,num=(select count(1) from tb where name=a.name) 
    from tb a  order by num desc,name
    /*
    id          name num         
    ----------- ---- ----------- 
    1           A    3
    2           A    3
    3           A    3
    4           B    2
    5           B    2
    6           C    2
    7           C    2
    8           D    1(所影响的行数为 8 行)
    */
    drop table tb
      

  3.   

    这个语句一定没问题:
    select name, count(*) from 表
    group by name order by count(*)
      

  4.   

    name不是只有ABCD,还有N多,能象自强那样写吗?
    还有稳步的查不出来啊?
    继续请教
      

  5.   


    wgsasd311(自强不息) ( )
    他的应该可以满足你的要求
      

  6.   

    create table tb(id int,name char)
    insert into tb
    select 1,'A' union all
    select 6,'C' union all
    select 5,'B' union all
    select 2,'A' union all
    select 7,'C' union all
    select 3,'A' union all
    select 8,'D' union all
    select 4,'B'select name,count(name) as num from tb group by name order by num desc
    /*
    name num         
    ---- ----------- 
    A    3
    B    2
    C    2
    D    1(所影响的行数为 4 行)
    */
    drop table tb