简体   繁体   English

MySQL试图一起使用INNER JOIN和UNION吗?

[英]MySQL trying to use INNER JOIN and UNION together?

so basically what I want to do is use INNER JOIN on two tables, but only selecting specific rows from Table A and the corresponding rows from Table B? 所以基本上我想做的是在两个表上使用INNER JOIN,但是只选择表A中的特定行和表B中的对应行?

Table A is called 'course', and I would want to select entries under the column 'course_title' that are called "Calculus," "British Literature I" and "Studio Art," as well as their corresponding 'department_id', and I would use 'department_id' as the reference for which to create an INNER JOIN with the second table, which is called 'departments,' which has the columns 'department_id' and 'department_name'. 表A被称为“课程”,我想在“ course_title”列下选择名为“微积分”,“英国文学I”和“工作室艺术”的条目,以及它们相应的“ department_id”和I将使用“ department_id”作为参考,使用第二个表(称为“ departments”)创建一个INNER JOIN,该表具有“ department_id”和“ department_name”列。

The only way I've thought to do this is by using a UNION to SELECT the three specific courses, but I can't figure out a way to get INNER JOIN to work with that? 我想到的唯一方法是使用UNION选择这三门特定的课程,但是我想不通一种方法来使INNER JOIN与之配合使用? I've tried several different types of syntax and keep getting errors, here is one of my attempts: 我尝试了几种不同类型的语法并不断出错,这是我的尝试之一:

(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;

Here's another one of my attempts: 这是我的另一尝试:

(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;

Any ideas on whether or not this would work? 关于这是否行得通的任何想法? If so, how do I correct my syntax? 如果是这样,如何纠正我的语法? If not, what's another method? 如果没有,还有什么方法?

Thank you!! 谢谢!!

you should use an IN clause... 您应该使用IN子句...

like this 像这样

WHERE course IN ('Calculus','Physics','Art')

You can write your query like below. 您可以像下面这样编写查询。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM