gpt4 book ai didi

mysql - SQL连接4张表

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:34 26 4
gpt4 key购买 nike

(SELECT s.sid, s.sname, e.ccode
FROM student s INNER JOIN enrolled e
ON s.sid = e.sid
WHERE s.programme = 'IT')
WHERE EXISTS
(SELECT c.ccode, c.tid
FROM class c INNER JOIN tutor t
ON c.tid = t.tid
WHERE t.tname = 'Hoffman');

这是我到目前为止所得到的,但不起作用。需要从 student 表中获取由名为 Hoffman 的导师注册的类(class)的 IT 学生的姓名,均来自 enrolled、class 和 tutor 表。我哪里错了?

类:

ccode   | cname         | weekday | meets at | room  | tid
________________________________________________________________________
CIS166 | Intro to Comp | Tue | 10:00:00 | MB300 | 1
________________________________________________________________________
CIS177 | Maths | Mon | 15:00:00 | MB100 | 6
________________________________________________________________________
CIS188 | Info Syst | Thu | 14:00:00 | MB230 | 5
________________________________________________________________________
CIS199 | Intro to Java | Fri | 10:00:00 | MB300 | 4
________________________________________________________________________
CIS277 | Algorithmics | Thu | 14:00:00 | MB200 | 2
________________________________________________________________________
CIS288 | Database Syst | Mon | 14:00:00 | MB142 | 1
________________________________________________________________________
CIS297 | Web Design | Mon | 16:00:00 | MB109 | 3
________________________________________________________________________
CIS298 | C++ | Fri | 10:00:00 | MB110 | 8

导师:

tid | tname
_____________________
1 | Rob Hoffman
_____________________
2 | James Butler
_____________________
3 | Kathy Bond
_____________________
4 | Theodora Stewart
_____________________
5 | Mike Richie
_____________________
6 | John Kay
_____________________
7 | Mary Tregear
_____________________
8 | Mark Robinson

已注册:

sid | ccode
______________
211 | CIS288
203 | CIS298
214 | CIS297
105 | CIS177
215 | CIS297
104 | CIS188
210 | CIS297
338 | CIS320
102 | CIS177
338 | CIS399
204 | CIS288
204 | CIS277
102 | CIS199
203 | CIS297
105 | CIS199
331 | CIS320
202 | CIS299
205 | CIS299
210 | CIS298
331 | CIS399
321 | CIS399
210 | CIS288
210 | CIS277
204 | CIS297
321 | CIS320
328 | CIS388
327 | CIS388
211 | CIS297
333 | CIS399
215 | CIS288
104 | CIS199

学生:

sid | sname        | programme | level | age
____________________________________________
101 | Lorry Ross | CS | 1 | 18
102 | Lydia Ken | CIS | 1 | 18
103 | Bob Chung | CS | 1 | 18
104 | Sonia Morris | CIS | 1 | 18
105 | Mark Poppy | CS | 1 | 19
106 | Megan Chi | IT | 1 | 20
218 | Diana McDon | IT | 2 | 20
219 | Nick Smith | IT | 2 | 21

最佳答案

在没有看到您的模式的情况下,这是一个猜测,但应该为您指明正确的方向:

select s.sid, s.sname, e.ccode 
from student s
inner join enrolled e on s.sid = e.sid
inner join class c on e.ccode = c.ccode --may be incorrect
inner join tutor t on c.tid = t.tid
where s.programme = 'IT'
and t.tname = 'Hoffman'

关于mysql - SQL连接4张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8563281/

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