简体   繁体   English

子句中的子选择是执行多次还是执行一次?

[英]Are sub-selects in a where-clauses executed multiple times or just once?

I have a separate table (with no PK, only one row) to store the row count of a certain result. 我有一个单独的表(没有PK,只有一行)来存储某个结果的行数。 Now, this count is used in a query, like this (just an example): 现在,此计数用于查询中,如下所示(仅作为示例):

SELECT * FROM `car`
WHERE `car`.`featured` = 1
AND ( SELECT `count` FROM `carfeaturedcounter` ) > 5;

Will SELECT count FROM carfeaturedcounter be executed multiple times (once for every row), or only once per query? SELECT count FROM carfeaturedcounter会执行多次(每行一次),还是每个查询一次?

I think it depends on the version of MySQL. 我认为这取决于MySQL的版本。 You might as well move the condition to the from clause, where it will only be executed once: 您最好将条件移至from子句,在该子句中将仅执行一次:

SELECT c.*
FROM car c.CROSS JOIN
     (SELECT `count` FROM carfeaturedcounter) x
WHERE c.featured = 1 AND x.`count` > 5;

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

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