我希望在一个mysql查询中显示按月计算的总数,每周总数,然后显示日数总数和天数。我可以使用单独的查询,但我想在单个查询中。是否可以显示此层次结构?
答案 0 :(得分:1)
使用GROUP BY MONTH我们发现按月分组和GROUP BY WEEK我们通过WEEK找到总数
Select sum(column) From table GROUP BY MONTH(column)
union
Select sum(column) From table GROUP BY WEEK(column)
和UNION运算符组合结果
答案 1 :(得分:0)
这是来自您提供的链接。
使用 UNION :
SELECT SUM(cost) AS total, MONTHNAME(date) AS month
FROM daily_expense
GROUP BY month
UNION
SELECT SUM(cost) AS total, CONCAT(date, ' - ', date + INTERVAL 6 DAY) AS week
FROM daily_expense
GROUP BY WEEK(date)