實際上 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中文網其他相關文章!