我的意思是这样:比如表中存储试卷答题结果,xx字段可能的值只有1 2 3 4,我要知道选1的有几个人,选2的有几个人,等等。用这个可以解决 select count(*) from table where xx = 1
但是由于字段很多,这样可能会比较慢。我想知道什么方法较好。

解决方案 »

  1.   

    select xx,count(*) from 表 group by xx
      

  2.   

    declare @t table(xx int)
    insert into @t select 1
    insert into @t select 2
    insert into @t select 3
    insert into @t select 4
    insert into @t select 4
    insert into @t select 2
    insert into @t select 3
    insert into @t select 3
    insert into @t select 4
    insert into @t select 2
    insert into @t select 1
    insert into @t select 1select xx,num=count(*) from @t group by xx order by xx/*
    xx          num         
    ----------- ----------- 
    1           3
    2           3
    3           3
    4           3
    */
      

  3.   

    非常感谢 libin_ftsafe(子陌红尘) 你给的这个语句可以在sql server中执行,但是不能在Delphi中执行。有没有标准的ansi sql? 谢谢。
      

  4.   

    应该是标准的SQL吧,呵呵:select 列名,count(*) from 表 group by 列名 order by 列名
      

  5.   

    我真笨。group by xx就行了。谢谢 libin_ftsafe(子陌红尘)