我有一个疑问,
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
关于MySQL 查询 - 对使用内部查询不感兴趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12930545/