gpt4 book ai didi

MySQL 按日期分组 DATE_FORMAT() 函数

转载 作者:行者123 更新时间:2023-11-29 05:58:37 26 4
gpt4 key购买 nike

我有以下查询:

SELECT DATE_FORMAT(order_completed, "%c/%Y") AS Month, COUNT(*) AS Total_Transactions, SUM(order_total_grand) as Total_Spend FROM `shop_orders` GROUP BY DATE_FORMAT(order_completed, "%c/%Y") DESC

输出下表:

Table

如何正确地按日期对表格进行排序?

最佳答案

不要按日期的字符串版本排序。按原始日期排序:

SELECT DATE_FORMAT(order_completed, "%c/%Y") AS Month, COUNT(*) AS Total_Transactions,
SUM(order_total_grand) as Total_Spend
FROM shop_orders
GROUP BY DATE_FORMAT(order_completed, "%c/%Y")
ORDER BY MIN(order_completed) DESC;

字符串按字符串排序,而不是日期。

关于MySQL 按日期分组 DATE_FORMAT() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47034059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com