我一直在尝试使用MySQL显示每周销售的结果表,其中一周的结果应该是:
Sun - Mon - Tues - Wed - Thur - Fri - Sat
所以我继续使用这个查询:
SELECT DATE_FORMAT(cart_calendar.datefield, '%d-%b') AS DATE
, IFNULL(SUM(cart_daily_sales.quantity),0) AS total_sales
FROM cart_daily_sales
RIGHT JOIN cart_calendar
ON (DATE(cart_daily_sales.order_date) = cart_calendar.datefield)
WHERE (cart_calendar.datefield)
GROUP BY WEEK(cart_calendar.datefield)
我知道我几乎就在那里,因为这会输出一年中所有的52周,我只想要当前一周的结果,而不是上面的代码导致...
'02-Jan', '09-Jan', '16-Jan', '23-Jan', '30-Jan', '06-Feb', '13-Feb', '20-Feb', '27-Feb', '06-Mar' e.t.c....
我知道我可以在今天的日期使用BETWEEN运算符,然后使用-7天但是这不会显示当前的一周,如果今天的星期三是星期三它将显示过去七周的最后几周,而不是正确的一周结构
如何显示上周每周分组的销售额?
答案 0 :(得分:2)
SELECT DATE_FORMAT(cc.datefield, '%d-%b') AS DATE
, SUM(IFNULL(cds.quantity,0)) AS total_sales
FROM cart_calendar cc
INNER JOIN cart_daily_sales cds
ON (DATE(cds.order_date) = cc.datefield)
WHERE WEEK(cc.datefield) = WEEK(now())
GROUP BY WEEK(cc.datefield)
或者如果你每天都想要它:
SELECT DATE_FORMAT(cc.datefield, '%d-%b') AS DATE
, SUM(IFNULL(cds.quantity,0)) AS total_sales
FROM cart_calendar cc
INNER JOIN cart_daily_sales cds
ON (DATE(cds.order_date) = cc.datefield)
WHERE WEEK(cc.datefield) = WEEK(now())
GROUP BY MOD(WEEKDAY(cc.datefield)+1,6) WITH ROLLUP