所以基本上我想要做的是在两个表上使用 INNER JOIN,但只从表 A 中选择特定行和从表 B 中选择相应的行?
表 A 称为“课程”,我想在“课程标题”列下选择名为“微积分”、“英国文学 I”和“工作室艺术”的条目,以及它们对应的“部门 ID”,我将使用“department_id”作为参考,为其创建与第二个表的 INNER JOIN,该表称为“departments”,该表具有“department_id”和“department_name”列。
我认为这样做的唯一方法是使用 UNION 来选择三个特定的课程,但我想不出一种方法让 INNER JOIN 与它一起工作?我尝试了几种不同类型的语法并不断出错,这是我的尝试之一:
(SELECT course.course_title, cours' at line 1
mysql> (SELECT course_title, department_id FROM course WHERE course_title = 'Calculus')
-> UNION
-> (SELECT course_title, department_id FROM course WHERE course_title = 'British Literature I')
-> UNION
-> (SELECT course_title, department_id FROM course WHERE course_title = 'Studio Art I')
-> UNION
-> (SELECT department_name, department_id FROM departments)
-> FROM departments INNER JOIN course ON departments.department_id = course.department_id;
这是我的另一个尝试:
(SELECT course.course_title, course.department_id, departments.department_id, departments.department_name WHERE course.course_title = 'Calculus')
-> UNION
-> (SELECT course.course_title, course.department_id, departments.department_id, departments.department_name WHERE course.course_title = 'British Literature I')
-> UNION
-> (SELECT course.course_title, course.department_id, departments.department_id, departments.department_name WHERE course.course_title = 'Studio Art I')
-> FROM departments INNER JOIN course ON departments.department_id = course.department_id;
关于这是否可行的任何想法?如果是这样,我该如何更正我的语法?如果不是,还有什么方法?
谢谢!!