由于这三个聚合来自具有相同WHERE
条件的同一张表,因此您不需要子选择。所有三个聚合都在同一个行分组上操作(没有GROUP BY
指定,所以整个表的一行),因此它们都可以SELECT
直接存在于列表中。
SELECT
SUM(number) AS number_sum,
MAX(number) AS number_max,
MIN(number) AS number_min
FROM `table`
如果任何聚合需要基于您将在WHERE
子句中过滤的不同条件,那么您将需要对不同条件使用子选择,或者进行笛卡尔连接。LEFT JOIN
对于仅返回一行的聚合,此子选择和以下方法在性能方面应该是等效的:
SELECT
/* Unique filtering condition - must be done in a subselect */
(SELECT SUM(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_sum,
MAX(number) AS number_max,
MIN(number) AS number_min
FROM `table`
或者等效于上面的查询,您可以LEFT JOIN
针对没有ON
子句的子查询。这只应在您知道子查询将仅返回一行的情况下进行。否则,您将得到一个笛卡尔积——连接一侧返回的行数乘以另一侧返回的行数。
如果您需要返回具有一组WHERE
子句条件的几列和具有一组不同条件的几列,WHERE
但仅从. 在这种情况下,它应该比使用相同子句执行两个子选择更快。JOIN
JOIN
WHERE
这应该更快....
SELECT
/* this one has two aggregates sharing a WHERE condition */
subq.number_sum_filtered,
subq.number_max_filtered,
/* ...and two aggregates on the main table with no WHERE clause filtering */
MAX(`table`.number) AS number_max,
MIN(`table`.number) AS number_min
FROM
`table`
LEFT JOIN (
SELECT
SUM(number) AS number_sum_filtered,
MAX(number) AS number_max_filtered
FROM `table`
WHERE `somecolumn = `somevalue`
) subq /* No ON clause here since there's no common column to join on... */
比这个...
SELECT
/* Two different subselects each over the same filtered set */
(SELECT SUM(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_sum_filtered,
(SELECT MAX(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_max_filtered,
MAX(`table`.number) AS number_max,
MIN(`table`.number) AS number_min
FROM
`table`