在使用 ROLLUP 修饰符时,可以使用 MySQL ORDER BY 子句对结果进行排序吗?
实际上,ROLLUP 和 ORDER BY 在 MySQL 中是互斥的,因此最好不要在查询中同时使用它们。但如果我们仍将 ROLLUP 用于 ORDER BY,主要劣势是汇总行连同它们计算出的行一同进行排序。还要注意,排序顺序将决定汇总行的位置。
汇总行将位于升序的开头和降序的结尾。查看以下示例以更加清楚地理解它 −
mysql> Select * from (Select sr, SUM(PRICE) AS PRICE from ratelist GROUP BY sr WITH ROLLUP) AS Price ORDER BY Sr DESC; +------+-------+ | sr | PRICE | +------+-------+ | 5 | 250 | | 4 | 850 | | 3 | 1005 | | 2 | 630 | | 1 | 502 | | NULL | 3237 | +------+-------+ 6 rows in set (0.00 sec) mysql> Select * from (Select sr, SUM(PRICE) AS PRICE from ratelist GROUP BY sr WITH ROLLUP) AS Price ORDER BY Sr ASC; +------+-------+ | sr | PRICE | +------+-------+ | NULL | 3237 | | 1 | 502 | | 2 | 630 | | 3 | 1005 | | 4 | 850 | | 5 | 250 | +------+-------+ 6 rows in set (0.01 sec)
广告