這是表我:SQL ORDER BY - 爲什麼它不在這裏工作?
+---------------------+--------+----------+
| date | sku | quantity |
+---------------------+--------+----------+
| 2017-08-23 14:58:00 | 123333 | 2 |
| 2017-08-23 14:58:00 | 123333 | 1 |
| 2017-08-23 14:58:00 | 123333 | 1 |
| 2017-08-23 14:58:00 | 123337 | 2 |
| 2017-08-23 14:58:00 | 123335 | 1 |
| 2017-08-23 14:58:00 | 123331 | 1 |
| 2017-08-23 14:58:00 | 123332 | 2 |
| 2017-08-23 14:58:00 | 123333 | 1 |
| 2017-08-23 14:58:00 | 123334 | 1 |
| 2017-08-23 14:58:00 | 123334 | 3 |
+---------------------+--------+----------+
我想如下執行select語句:
select sku,sum(quantity) as 'total quant' from transactions
where DATE(date) between '2017-07-23' and '2017-09-23'
group by sku
order by quantity desc;
但我得到這個:
+--------+-------------+
| sku | total quant |
+--------+-------------+
| 123332 | 2 |
| 123337 | 2 |
| 123333 | 5 |
| 123334 | 4 |
| 123335 | 1 |
| 123331 | 1 |
+--------+-------------+
哪一項不是按我期待的順序排列。 爲什麼訂單不在這裏工作?
邊注我quantity
列的數據類型爲int
的,我已經試過鑄造在this post但無濟於事建議。
難道你不想通過總量來訂購嗎? –
該ORDER BY無效。應該引發錯誤... – jarlh
執行'ORDER BY'total quant'desc'。 – jarlh