基于第一个多响应的第二个表上的SQL查询

时间:2012-06-18 03:27:16

标签: sql

如果第一个返回多个值,那么这是一个SQL查询失败 -

SELECT     subject
FROM         a_batchcourse_subject AS s
WHERE     (batchcourse =
                          (SELECT     batchcourse
                          FROM          a_course_batchcourse AS b
                          WHERE      (course = 'Course 1')))

在第一个查询可能返回多个值的情况下,编写此查询的正确方法是什么?

2 个答案:

答案 0 :(得分:5)

您希望sql中的IN不是=

SELECT subject
FROM a_batchcourse_subject AS s
WHERE batchcourse IN
(SELECT batchcourse
FROM a_course_batchcourse AS b
WHERE course = 'Course 1')

答案 1 :(得分:4)

JOININ子句

快得多
SELECT  s.subject
FROM    a_batchcourse_subject s 
        INNER JOIN a_course_batchcourse b
        on s.batchcourse = b.batchcourse
WHERE   (b.course = 'Course 1')