2

以下代码不起作用,但我使用的是我要完成的示例:

SELECT thing_id
FROM table_of_things
WHERE table_of_things.thing_color, table_of_things.thing_flavor IN 
                          ( SELECT good_color,good_flavor FROM good_things )

编辑:很明显,我的代码对某些人来说很明显。我正在尝试使用子查询中的两个条件从 table_of_things 中获取 things_id。

4

2 回答 2

6

您尚未提出问题,但我怀疑您需要将查询更改为以下内容:

SELECT thing_id
FROM table_of_things
WHERE table_of_things.thing_color IN  ( SELECT good_color FROM good_things )
AND table_of_things.thing_flavor  IN  ( SELECT good_flavor FROM good_things )

或许:

SELECT thing_id
FROM table_of_things as tot
INNER JOIN good_things as gt
ON tot.thing_color = gt.good_color AND tot.thing_flavor = gt.good_flavor

in 语句只能检查一个值,而不是像您尝试做的那样检查两个值。

于 2012-09-12T18:17:31.997 回答
1

使用连接

SELECT t.thing_id
FROM table_of_things t
JOIN good_things gt 
ON t.thing_color = gt.good_color 
AND t.thing_flavor =  gt.good_flavor

使用联合

SELECT t.thing_id FROM table_of_things t WHERE t.thing_color IN ( SELECT good_color FROM good_things )
UNION 
SELECT t.thing_id FROM table_of_things t WHERE t.thing_flavor IN ( SELECT good_flavor FROM good_things )

使用 EXIST

SELECT t.thing_id FROM table_of_things t 
WHERE EXISTS ( SELECT good_color,good_flavor FROM good_things)
于 2012-09-13T03:24:04.383 回答