数据库
首页 > 数据库> > MySQL查询执行2张表之间的联接

MySQL查询执行2张表之间的联接

作者:互联网

这些是表格:

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 |
+-------+--------+--------+--------+------+

works
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
|        1 |     1 |      10 |
|        2 |     2 |       9 |
|        3 |     3 |       8 |
|        4 |     4 |      10 |
|        5 |     5 |       9 |
|        6 |     1 |       9 |
+----------+-------+---------+

上面是我需要从中检索数据的表.

The question is to return list of Employees who take both Class 10 and
Class 9.

我写的查询是:

select professor.name 
from inner join works
on professor.empid=works.empid
where works.classid=9 and works.classid=10;

我知道我想要的结果是Arun,但是我不知道应该使用什么确切的查询来检索所需的结果.

解决方法:

他希望教授9级和10级的教授.因此,作品中有2条不同的记录需要匹配.

select professor.name from  professor
join works A on A.empid=professor.empid and A.classid=9
join works B on B.empid=professor.empid and B.classid=10

查看http://sqlfiddle.com/#!2/4be88a/1

标签:inner-join,mysql
来源: https://codeday.me/bug/20191122/2057679.html