基本上我想要做的是在两个表上使用INNER JOIN,但只选择表A中的特定行和表B中的相应行?
表A被称为“课程”,我想在“course_title”栏目下选择名为“微积分”,“英国文学I”和“工作室艺术”的条目,以及相应的“department_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;
关于这是否有效的任何想法?如果是这样,我该如何更正我的语法?如果没有,那么另一种方法是什么?
谢谢!
答案 0 :(得分:2)
你应该使用IN子句......
像这样WHERE course IN ('Calculus','Physics','Art')
答案 1 :(得分:2)
您可以像下面一样编写查询。
SELECT course.course_title, course.department_id, departments.department_id, departments.department_name
FROM departments
INNER JOIN course ON departments.department_id = course.department_id
WHERE course.course_title IN ('Calculus', 'British Literature I','Studio Art I')