如何使用返回2列的子查询执行IN?

时间:2014-04-24 23:36:35

标签: sql

我有这个查询

SELECT *
FROM GUITARS.FENDER
WHERE FENDER.GUITARTYPE IN (
SELECT GUITARTYPE,GUITARSUBTYPE
FROM GUITARS.GUITAR_TYPE
WHERE GuitarColor = 'RED')

当我只在子查询中执行GUITARTYPE而不是GUITARTYPE,GUITARSUBTYPE时,我可以运行它。

1 个答案:

答案 0 :(得分:2)

请勿使用in,请使用exists

SELECT *
FROM GUITARS.FENDER
WHERE EXISTS (SELECT 1
              FROM GUITARS.GUITAR_TYPE
              WHERE GuitarColor = 'RED' AND
                    FENDER.GUITARTYPE = GUITARTYPE.GUITARTYPE AND
                    FENDER.GUITARSUBTYPE = GUITARTYPE.GUITARSUBTYPE
             );