where-clauses中的子选择是多次执行还是只执行一次?

时间:2016-07-13 11:40:21

标签: mysql

我有一个单独的表(没有PK,只有一行)来存储某个结果的行数。现在,这个计数用在一个查询中,就像这样(只是一个例子):

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

SELECT count FROM carfeaturedcounter会多次执行(每行一次),还是每次查询只执行一次?

1 个答案:

答案 0 :(得分:0)

我认为这取决于MySQL的版本。您也可以将条件移动到from子句,它只会执行一次:

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