MySQL查询-对使用内部查询不感兴趣
我有一个疑问MySQL查询-对使用内部查询不感兴趣,mysql,sql,join,Mysql,Sql,Join,我有一个疑问 select col1,col2 from table1; 返回两列多行。我想在另一个查询的where条件中使用这两个值。差不多 select col3,col4 from table2 where col5=col1 and col6=col2; select col3,col4 from table2 where col5 in (select col1 from table1) and col6 in (select col2 from table1); 其中c
select col1,col2 from table1;
返回两列多行。我想在另一个查询的where条件中使用这两个值。差不多
select col3,col4 from table2 where col5=col1 and col6=col2;
select col3,col4 from table2
where col5 in (select col1 from table1)
and col6 in (select col2 from table1);
其中col1
和col2
是第一次查询的结果值
目前,我使用了内部查询,比如
select col3,col4 from table2 where col5=col1 and col6=col2;
select col3,col4 from table2
where col5 in (select col1 from table1)
and col6 in (select col2 from table1);
但是我不想使用上面所示的内部查询,因为它会减慢结果的速度
请建议。
加入他们,而不是像这样在
中使用:
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
注意,您不需要在第二个表中选择特定的列。您可以像这样直接加入第二个表table1
:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN table1 t1 ON t2.col5 = t1.col1
AND t2.col6 = t1.col2
谢谢,成功了。我已经使用了第一个查询,因为我在已经存在的查询中有很多复杂性。