例如:
SELECT tb1.column1, tb1.column2
FROM table1 AS tb1
WHERE tb1.column1 = (SELECT tb2.column2 FROM table2 As tb2)
但是……子查询可能有不止一个结果。如果有 3 个结果来自子查询,我该如何处理?
用于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
您需要使用in
而不是=
:
SELECT tb1.column1, tb1.column2
FROM table1 AS tb1
WHERE tb1.column1 in (SELECT tb2.column2 FROM table2 As tb2)