mysql内连接有

时间:2014-02-01 12:28:58

标签: mysql join group-by having

我的表是:

mysql> select * from professor;
    +-------+--------+--------+--------+------+
    | empid | name   | status | salary | age  |
    +-------+--------+--------+--------+------+
    |     1 | Arun   |      1 |   2000 |   23 |
    |     2 | Benoy  |      0 |   3000 |   25 |
    |     3 | Chacko |      1 |   1000 |   36 |
    |     4 | Divin  |      0 |   5000 |   32 |
    |     5 | Edwin  |      1 |   2500 |   55 |
    |     7 | George |      0 |   1500 |   46 |
    +-------+--------+--------+--------+------+
    6 rows in set (0.00 sec)

mysql> select * from works;
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
|        1 |     1 |      10 |
|        2 |     2 |       9 |
|        3 |     3 |       8 |
|        4 |     4 |      10 |
|        5 |     5 |       9 |
|        6 |     1 |       9 |
|        2 |     3 |      10 |
|        2 |     1 |       7 |
|        4 |     2 |       6 |
|        2 |     4 |       6 |
|        2 |     5 |       2 |
|        7 |     5 |       6 |
|        3 |     5 |       2 |
|        6 |     4 |      10 |
+----------+-------+---------+
14 rows in set (0.00 sec)

mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
|        1 | Maths      |      5 |
|        2 | Science    |      1 |
|        3 | English    |      6 |
|        4 | Social     |      4 |
|        5 | Malayalam  |     20 |
|        6 | Arts       |     25 |
|        7 | Biology    |     20 |
+----------+------------+--------+
7 rows in set (0.00 sec)

问题是:

  

列出那些没有教过三门课程的教授   班级7,8,9

我的查询是:

select professor.name from professor
inner join works
on professor.empid=works.empid
group by works.empid
having works.classid not in (7,8,9);

我得到的输出是:

Arun    
Divin   

我实际需要获得的输出:

Divin住宿

请帮助我。

http://sqlfiddle.com/#!2/77ec7/46

3 个答案:

答案 0 :(得分:1)

添加

group by works.empid, works.classid

答案 1 :(得分:0)

select professor.name from professor
inner join works
on professor.empid=works.empid
inner join course on work.courseid=course.courseid
group by works.empid
having works.classid not in (7,8,9);

答案 2 :(得分:0)

试试这个

 select name  from professor where name not in (

 select name  from professor
 inner join works
 on professor.empid=works.empid

 where works.classid  in (7,8,9)
 group by  professor.name );

你会得到

 divin
 george
乔治也没有7,8,9