我有以下sql:
SELECT CONCAT('', DAY(OrderUpdated), '') AS OrderDate, SUM(OrderTotal) AS ChartOrderTotal
FROM Orders WHERE OrderUpdated >= '2018-1-1' AND OrderUpdated <= '2018-2-1'
GROUP BY CONCAT(LEFT(MONTHNAME(OrderUpdated), 3), ' ', DAY(OrderUpdated), ',', YEAR(OrderUpdated)), DAYOFWEEK(OrderUpdated)
ORDER BY DAY(OrderUpdated) ASC
SQL上方显示的结果是:
2, 20000
5, 2000
7, 8200
15, 1300
20, 1200
现在,如果我希望所有丢失的日子也都归零。
如:
1, 0
2, 20000
3, 0
4, 0
....
20,1200
21, 0
22, 0
....
同样在哪里我使用手动输入的日期。
如何在WHERE CLAUSE中选择当前月份。
仅供参考:OrderUpdated日期为CURRENT_TIMESTAMP
最佳答案
您还应该按ID进行分组,以便获取所有值:
SELECT CONCAT('', DAY(OrderUpdated), '') AS OrderDate, SUM(OrderTotal) AS ChartOrderTotal
FROM Orders WHERE OrderUpdated >= '2018-1-1' AND OrderUpdated <= '2018-2-1'
GROUP BY CONCAT(LEFT(MONTHNAME(OrderUpdated), 3), ' ', DAY(OrderUpdated), ',', YEAR(OrderUpdated)), DAYOFWEEK(OrderUpdated), ID
ORDER BY DAY(OrderUpdated) ASC
关于php - 当月总销售额,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48765435/