id name subject
insert into teacher values(1,'张三','java');
insert into teacher values(2,'王五','C#');
insert into teacher values(3,'李四','java');
insert into teacher values(4,'张三','C#');
insert into teacher values(5,'李四','php');
insert into teacher values(6,'张三','php');
insert into teacher values(7,'李四','C#');
insert into teacher values(8,'张三','java');
insert into teacher values(9,'王五','java');
.............有约一万条数据,里面有很多重复的怎么查询所教课程三门以上的老师的姓名

解决方案 »

  1.   


    create table teacher(id int,name varchar2(20),subject varchar2(20));
    insert into teacher values(1,'张三','java');
    insert into teacher values(2,'王五','C#');
    insert into teacher values(3,'李四','java');
    insert into teacher values(4,'张三','C#');
    insert into teacher values(5,'李四','php');
    insert into teacher values(6,'张三','php');
    insert into teacher values(7,'李四','C#');
    insert into teacher values(8,'张三','java');
    insert into teacher values(9,'王五','java');
    insert into teacher values(10,'李四','oracle');--根据姓名分组
    select name from teacher group by name having count(distinct subject) > 3
    NAME
    --------------------
    李四
      

  2.   

    select  name,count(subject) from teacher where count(subject) > 3 group by name 
      

  3.   

    这个不行 count(subject)>3这里不能用这个函数
      

  4.   

    select name from (select  name,count(subject) as total from tab  group by name) a where a.total > 3
      

  5.   

    select * from (
    select a.*,row_number(partition by name order by 1) rn   from teacher a  
    ) where rn >=3
      

  6.   


    select name from techer group by name having count(subject)>3
    同意一楼
      

  7.   

    我在4l写的语法有问题?烦请高手指教下!至于2L中count确实不能用于WHERE中,这两条代码是昨天我在宿舍写的,手头没有ORACEL或其它来测试语法。个人感觉4L的语法在SQL SERVER是没有问题的,ORACLE应该也支持。偶是一角用户,新手烦请大家多多指教!
      

  8.   

    select name from (select  name,count(subject) as total
     from teacher  group by name) a where a.total > 3以L楼的测试数据,所查询出来的结果是
    ----------------------------------------------
    李四
    张三
    ----------------------------------------------
    测试没有问题
      

  9.   

    select name from (select name,count(distinct subject) as total 
     from teacher  group by name) a where a.total > 3
    这个也是正确的。
    -----------
    李四
    -----------
    没看到前面说有重复。是掉了点东东.
      

  10.   

    现在突然发现xiedi1209就是一个非常蛋疼的人。别人把实现方式写出来,不管好与不好,都可以参考学习一下。