gpt4 book ai didi

mysql - 给出所有不教数学的老师的名字

转载 作者:行者123 更新时间:2023-11-29 22:33:06 25 4
gpt4 key购买 nike

我有两张 table 。第一个是Course 表,第二个是Teacher 表。我想要找到所有不教'数学'老师。我怎样才能做到这一点?

类(class)表

course_id   course  teacher_id  marks
1 Physics 1 60
2 Math 1 60
3 Chemestry 1 60
4 English 2 60
5 Hindi 2 60
6 Physics 2 60
7 Chemestry 3 60
8 English 4 60
9 Math 5 60
10 Math 6 60

教师表

teacher_id  name      salary    gender
1 Teacher1 20 1
2 Teacher2 30 1
3 Teacher3 40 2
4 Teacher4 50 2
5 Teacher5 60 1
6 Teacher6 70 2

最佳答案

I want to get all teacher who does not teachs math.

您需要连接 teacher_id 上的两个表,然后根据类(class)过滤掉行。

SQL> SELECT DISTINCT t.name
2 FROM course c,
3 teacher t
4 WHERE c.teacher_id = t.teacher_id
5 AND c.course <> 'Math';

NAME
--------
Teacher2
Teacher1
Teacher4
Teacher3

SQL>

编辑由于您有教授多门类(class)的老师,因此您需要进一步过滤:

SQL> WITH DATA AS
2 (SELECT c.*,
3 t.name
4 FROM course c,
5 teacher t
6 WHERE c.teacher_id = t.teacher_id
7 AND c.course <> 'Math'
8 )
9 SELECT DISTINCT name
10 FROM data
11 WHERE teacher_id NOT IN
12 (SELECT teacher_id FROM course WHERE course = 'Math'
13 )
14 /

NAME
--------
Teacher2
Teacher4
Teacher3

SQL>

注意请记住,使用NOT EXISTS子句的另一种解决方案在性能方面更好,因为表扫描更少,甚至索引扫描也更少。有了适当的索引,不存在查询将是最佳方法。

关于mysql - 给出所有不教数学的老师的名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com