Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询-对使用内部查询不感兴趣_Mysql_Sql_Join - Fatal编程技术网

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

谢谢,成功了。我已经使用了第一个查询,因为我在已经存在的查询中有很多复杂性。