0

我想按查询中的方式对记录进行排序:

SELECT `month`, SUM(order_quantity) AS order_quantity FROM" +
            "(  SELECT MONTH(date_livraison) AS month,         COUNT(*) AS order_quantity  FROM" +
            " bon_livraison    GROUP BY MONTH(date_livraison)    UNION ALL      " +
            "SELECT MONTH(DATE_SUB(NOW(), INTERVAL 12 MONTH)) AS month, 0 order_quantity      UNION ALL     " +
            " SELECT MONTH(DATE_SUB(NOW(), INTERVAL 11 MONTH)) AS month, 0 order_quantity     UNION ALL  " +
            "   SELECT MONTH(DATE_SUB(NOW(), INTERVAL 10 MONTH)) AS month, 0 order_quantity     UNION ALL  " +
            "   SELECT MONTH(DATE_SUB(NOW(), INTERVAL 9 MONTH)) AS month, 0 order_quantity    UNION ALL  " +
            "  SELECT MONTH(DATE_SUB(NOW(), INTERVAL 8 MONTH)) AS month, 0 order_quantity     UNION ALL  " +
            "   SELECT MONTH(DATE_SUB(NOW(), INTERVAL 7 MONTH)) AS month, 0 order_quantity     UNION ALL  " +
            "   SELECT MONTH(DATE_SUB(NOW(), INTERVAL 6 MONTH)) AS month, 0 order_quantity     UNION ALL   " +
            "  SELECT MONTH(DATE_SUB(NOW(), INTERVAL 5 MONTH)) AS month, 0 order_quantity     UNION ALL    " +
            " SELECT MONTH(DATE_SUB(NOW(), INTERVAL 4 MONTH)) AS month, 0 order_quantity     UNION ALL    " +
            " SELECT MONTH(DATE_SUB(NOW(), INTERVAL 3 MONTH)) AS month, 0 order_quantity     UNION ALL   " +
            "  SELECT MONTH(DATE_SUB(NOW(), INTERVAL 2 MONTH)) AS month, 0 order_quantity     UNION ALL  " +
            "   SELECT MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AS month, 0 order_quantity ) a GROUP BY `month` ORDER BY `month`

但我总是有 1....12

你知道吗

**************************编辑

这是我放置order by 'month desc'或添加新列并按它排序时的结果: 在此处输入图像描述

如果你看,问题是 order_quantity 的顺序具有第一优先级

4

0 回答 0