一个表有字段
id   test
1     a
2     b
3     c
4     d
5     a
如何判断是否有两条a的数据在表中呢?
我是写在存储过程里面的

解决方案 »

  1.   

    select count(*) from table group by test
      

  2.   

    --判断有几个a
    select count(*) from tb where test='a' group by test
      

  3.   

    --判断有几个a:
    select count(*) from tb where test='a'
    --判断哪几个有重复,重复多少:
    select test,count(*) from tb group by test having count(*)>1
      

  4.   

    create table tb
    (
    id int,
    test nvarchar(50)
    )insert into tb
    select 1,    'a' union all
    select 2,    'b' union all 
    select 3,    'c' union all 
    select 4,    'd' union all 
    select 5,    'a' if exists(
    select 1
    from tb 
    where test = 'a' 
    group by test 
    having count(test) = 2
    )
    begin
    print 'yes'
    enddrop table tb
    结果:
    yes
      

  5.   

    SELECT COUNT(*) FROM TABLE WHERE TEST='A' GROUP BY TEST