我有一个查询说,
select col1,col2 from table1;
返回 2 列多行。我想在另一个查询的 where 条件中使用这两个值。类似的东西
select col3,col4 from table2 where col5=col1 and col6=col2;
其中col1
和col2
是第一个查询的结果值。
目前我已经使用了类似内部查询的东西
select col3,col4 from table2
where col5 in (select col1 from table1)
and col6 in (select col2 from table1);
但我不想使用如上所示的内部查询,因为它会减慢带来结果的速度。
请指教。
JOIN
它们,而不是像这样使用IN
:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN
(
SELECT col1, col2
FROM table1
) t1 ON t2.col5 = t1.col1 AND t2.col6 = t1.col2
请注意,您无需选择第二个表中的特定列。您可以像这样直接JOIN
第二个表table1
:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN table1 t1 ON t2.col5 = t1.col1
AND t2.col6 = t1.col2