实际上 ROLLUP 和 ORDER BY 在 MySQL 中是互斥的,因此在查询中同时使用它们并不是一个好的做法。但是,如果我们在 ORDER BY 中使用 ROLLUP,那么主要的缺点是汇总行将与它们计算的行一起排序。同样值得注意的是,排序顺序将决定摘要行的位置。
摘要行将位于升序的开头和降序的结尾。请考虑以下示例以更清楚地理解它 -
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)
以上是使用 ROLLUP 修饰符时,是否可以使用 MySQL ORDER BY 子句对结果进行排序?的详细内容。更多信息请关注PHP中文网其他相关文章!