select * from tb where not exists((select count(id) from tb)<1)后面查出的结果进行比较??可以实现这样的语句不??

解决方案 »

  1.   

    不行是肯定的,用having可以比较好的解决。
      

  2.   

    NO 应该返回整个tb你想查什么
      

  3.   

    我的意思是这样 有字段表示性别 
    select  1   from   tb   where   not   exists((select   count(id)   from   tb where xb='雄性') !=1) 雄性只能一只 多或者没有就不符合条件
      

  4.   

    试试看!
    select   *   from   tb   where   not   exists (select count(id) as  id from tb where   id<1)
      

  5.   

    不可以那样写:
    select * form tb t where (select count(1) from tb where xb=t.xb)=1
      

  6.   


    用这个试下
    select   id   from tb where xb= '雄性 '  group by id
    having count(id)<1 
      

  7.   

    写在having 的 count是计算ID select count(id)是有区别的!
      

  8.   

    declare @T table (id int, xb char(4))
    insert @T select 1,'雌性'
    insert @T select 2,'雄性'
    insert @T select 3,'雄性'
    insert @T select 3,'雌性'
    insert @T select 3,'雌性'
    insert @T select 4,'雄性'
    insert @T select 4,'雄性'
    insert @T select 4,'雌性'--雄性只能一只 多或者没有就不符合条件
    select * from @T a
    where
    (select count(id) from @T where id=a.id and xb='雄性')=1
    /*
    2 雄性
    3 雄性
    3 雌性
    3 雌性
    */--并且最少要有一个雌性
    select * from @T a
    where
    (select count(id) from @T where id=a.id and xb='雄性')=1
    and exists (select 1 from @T where id=a.id and xb='雌性')
    /*
    3 雄性
    3 雌性
    3 雌性
    */
      

  9.   

    关键是要了解sql的执行顺序,就像了解加减乘除一样