请问大家,如何对oracle子查询出来的结果进行判断,例如,
select p.* from user p where (select count(*) from user q group by q.class)>2;
就是如何对红色区域进行判断,我写的>2会报错,因为返回的是多个结果,所以大于2肯定不对,大家有什么解决方法?谢谢!

解决方案 »

  1.   

    select count(*) from user q group by q.class having count(*)>2
      

  2.   

    select p.* from user p where exists (select count(*) from user q where q.class=p.class group by q.class having count(*)>2)
      

  3.   

    日好有错再改下
    select p.* from user p where exists (select q.class,count(*) from user q where q.class=p.class group by q.class having count(*)>2)
      

  4.   

    大哥,其实我的真实需求是这样的,能否看一下,我用having不行啊
    select t.* from peopleexaminfo t where t.healthsn in (select p.healthsn from peopleexaminfo p where 
    (select count(*) from peopleexaminfo x where x.result=1 group by x.healthsn having count(*)>2))
      

  5.   

    select count(*) t from (select count(*) t from peopleexaminfo x where x.result=1 group by x.healthsn )  where t>2
      

  6.   

    你是用Hibernate吧,from后边必须是对象,可以参考http://kuangbaoxu.javaeye.com/blog/193076
      

  7.   


    你那语句有几个地方都有语法错误
    1.你的group by 分组了,但是你的查询字段里没有
    select x.healthsn,count(*) from peopleexaminfo x where x.result=1 group by x.healthsn having count(*)>2
    2.你的这个红色区域的子查询和外层有什么关系呢?不然你用where有什么作用?
      

  8.   

    写错了
    应该是select count(*)  from (select count(*) t from peopleexaminfo x where x.result=1 group by x.healthsn ) t where t>2
      

  9.   

    是的,我也觉得您说的第二条很有道理,但是第一条,我记得group by 后面跟的可以在查询条件里面没有,但是查询条件里面有的一定要在groupby 中呢
      

  10.   

    select t.* from peopleexaminfo t where t.healthsn in (select p.healthsn from peopleexaminfo p where
    p.healthsn=x.healthsn and 
    (select count(*) from peopleexaminfo x where x.result=1 group by x.healthsn having count(*)>2))
    好像应该加上这部分
      

  11.   


    要不你可以试验下,
    你可以假想下,如果你按id分组了,现在id有1和2,按照count(1)统计,统计分别为4,5.
    你说就依照select count(1) from table group by id;
    你说oracle 怎么显示呢?