内部联接上的内部联接-该列被多次指定

42fyovps  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(357)

我有如下表格:

表的顺序是“学生”、“课程”和“课程”
我在使用连接收集数据时遇到了一些问题。我在找至少考过一次的学生的名字。科学院。当然。
我的问题是:

SELECT S.name
FROM student S
INNER JOIN (SELECT * FROM takes INNER JOIN course) AS C 
ON S.ID = C.ID AND C.dept_name= 'Comp. Sci.'

但是,执行时出现错误:

Msg 102, Level 15, State 1, Line 32
Incorrect syntax near ')'.

还有一个红色下划线“as c”表示:

The column 'course_id' was specified multiple times for 'C'

我们不应该使用自然连接,因为我们只学习了基本的连接,如内连接、外连接、右连接、左连接、交叉连接等。
有人能帮我找出为什么我会出现这些错误,并进行调试吗?

zpf6vheq

zpf6vheq1#

你的 inner join 不正确,请在此处阅读有关加入的更多信息。请尝试以下操作。

SELECT 
   s.name,
   t.*
FROM student s
INNER JOIN takes t 
on s.id = t.id
INNER JOIN course c 
ON t.course_id = c.course_id
WHERE c.dept_name = 'Comp. Sci.'
dgjrabp2

dgjrabp22#

使用 exists 如果你只是想让学生们:

select s.*
from students s
where exists (select 1
              from takes t join
                   course c
                   on t.course_id = c.course_id
              where t.id = s.id and t.dept_name = 'Comp Sci.'
             );

exists ,您不必担心重复。

相关问题