例如:
SELECT tb1.column1, tb1.column2
FROM table1 AS tb1
WHERE tb1.column1 = (SELECT tb2.column2 FROM table2 As tb2)
但是......子查询可能有多个结果。如果子查询有3个结果,我该如何处理?
答案 0 :(得分:2)
在此使用IN
。
IN
可以处理多个值,而=
只能容纳一个。
SELECT tb1.column1, tb1.column2
FROM table1 AS tb1
WHERE tb1.column1 IN (SELECT tb2.column2 FROM table2 As tb2)
或另一种方法是使用JOIN
,这是更优选的,
SELECT DISTINCT tb1.column1, tb1.column2
FROM table1 AS tb1
INNER JOIN table2 b
ON tb1.column1 = b.column2
答案 1 :(得分:1)
您需要使用in
而不是=
:
SELECT tb1.column1, tb1.column2
FROM table1 AS tb1
WHERE tb1.column1 in (SELECT tb2.column2 FROM table2 As tb2)