Mysql子查询转换内连接

时间:2016-07-05 07:54:15

标签: mysql join

我已经读过加入请求通常比子查询更有效。

我想知道下面的查询是否可以转换为加入。 Personnaly,我不知道这是怎么可能的。

只是让你知道,距离的计算是昂贵的,我只想在子集上执行它(子查询的结果)。但是,外部查询不会保持"顺序为"。这就是为什么我必须在外部查询上执行第二个订单。

有没有办法做得更好?

 SELECT alias.*, [calculation of distance using table cities] AS distance
 FROM ( SELECT item.*  FROM item  
        WHERE  item.postalCode IN ([Array of postal codes]) 
        ORDER BY item.creationTimestamp ASC LIMIT 0, 19 ) as myalias,
       country_cities 
 WHERE cities.postalCode = alias.postalCode 
 ORDER BY myalias.creationTimestamp ASC

1 个答案:

答案 0 :(得分:0)

您可以将其更改为内部加入,如下所示

SELECT alias.*, [calculation of distance using table cities] AS distance
 FROM ( SELECT item.*  FROM item  
        WHERE  item.postalCode IN ([Array of postal codes]) 
        ORDER BY item.creationTimestamp ASC LIMIT 0, 19 ) myalias INNER JOIN country_cities  
 WHERE cities.postalCode = alias.postalCode 
 ORDER BY myalias.creationTimestamp ASC