简体   繁体   English

MySQL子查询转换内部联接

[英]Mysql subquery conversion inner join

I've read that join request are generally more efficient that subquery. 我读过,加入请求通常比子查询更有效。

I was wondering if the query below could be converted to join. 我想知道下面的查询是否可以转换为联接。 Personnaly, I don't see how this is possible. 从个性上讲,我不知道这是怎么可能的。

Just for you to know, the calculation of distance being expensive, I just want to perform it on a subset (the result of the subquery). 众所周知,距离的计算非常昂贵,我只想对一个子集(子查询的结果)执行该操作。 But then, the outer query is not keeping the "order by". 但随后,外部查询不会保留“ order by”。 This is why I must do a second order by on the outer query. 这就是为什么我必须对外部查询执行第二次排序的原因。

Is there a way to do that better ? 有什么办法可以做得更好?

 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

YOU CAN CHANGE IT TO INNER JOIN as follows 您可以按以下方式将其更改为内联接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM