【问题标题】:mysql inner join with having [closed]mysql内部连接有[关闭]
【发布时间】:2014-02-01 12:28:57
【问题描述】:

我的桌子是:

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   

我实际需要得到的输出:

占卜

请帮帮我。

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

【问题讨论】:

  • 我认为您应该自己解决这些问题。不然还有什么意义?
  • 所以,这似乎是家庭作业
  • 我认为乔治也没有 7,8,9 或?

标签: mysql join group-by having


【解决方案1】:

添加

group by works.empid, works.classid

【讨论】:

    【解决方案2】:
    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);
    

    【讨论】:

      【解决方案3】:

      试试这个

       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

      【讨论】:

        猜你喜欢
        • 2012-09-04
        • 2014-12-09
        • 2018-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2017-07-07
        • 1970-01-01
        相关资源
        最近更新 更多