MySql Single Table,最后7天select并包含空行

我在这里search了类似的问题在stackoverflow,但我不明白如何使这项工作,我正在试图做…

所以,我想从数据库中获得最近7天的交易,并获得总销售额,如果某天没有数据,也包括空行。

我到目前为止: http : //sqlfiddle.com/#!2/f4eda/6

这输出:

| PURCHASE_DATE | AMOUNT | |---------------|--------| | 2014-04-25 | 19 | | 2014-04-24 | 38 | | 2014-04-22 | 19 | | 2014-04-19 | 19 | 

我想要的是:

 | PURCHASE_DATE | AMOUNT | |---------------|--------| | 2014-04-25 | 19 | | 2014-04-24 | 38 | | 2014-04-23 | 0 | | 2014-04-22 | 19 | | 2014-04-21 | 0 | | 2014-04-20 | 0 | | 2014-04-19 | 19 | 

任何帮助赞赏:)

3 Solutions collect form web for “MySql Single Table,最后7天select并包含空行”

简单地把你想要的date和使用left outer join的子查询放在一起:

 select d.thedate, coalesce(SUM(amount), 0) AS amount from (select date('2014-04-25') as thedate union all select date('2014-04-24') union all select date('2014-04-23') union all select date('2014-04-22') union all select date('2014-04-21') union all select date('2014-04-20') union all select date('2014-04-19') ) d left outer join transactions t on t.purchase_date = d.thedate and vendor_id = 0 GROUP BY d.thedate ORDER BY d.thedate DESC; 

这并不容易。 我从这个线程中获得了帮助, 从date范围中生成了几天,并将其与查询结合起来。

所以我们的想法是获得最近7天的date列表,然后将这些date的静态数量0join到您的查询中,然后总结它们。 这可以用于任何date范围,只需要在两个查询中改变它们

 select t1.purchase_date, coalesce(SUM(t1.amount+t2.amount), 0) AS amount from ( select DATE_FORMAT(a.Date,'%Y-%m-%d') as purchase_date, '0' as amount from ( select curdate() - INTERVAL (aa + (10 * ba) + (100 * ca)) DAY as Date from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c ) a where a.Date BETWEEN NOW() - INTERVAL 7 DAY AND NOW() )t1 left join ( SELECT DATE_FORMAT(purchase_date, '%Y-%m-%d') as purchase_date, coalesce(SUM(amount), 0) AS amount FROM transactions WHERE purchase_date BETWEEN NOW() - INTERVAL 7 DAY AND NOW() AND vendor_id = 0 GROUP BY purchase_date )t2 on t2.purchase_date = t1.purchase_date group by t1.purchase_date order by t1.purchase_date desc 

DEMO

这是过去7天;

 select d.thedate, coalesce(SUM(amount), 0) AS amount from (select DATE(NOW()) as thedate union all select DATE(DATE_SUB( NOW(), INTERVAL 1 DAY)) union all select DATE(DATE_SUB( NOW(), INTERVAL 2 DAY)) union all select DATE(DATE_SUB( NOW(), INTERVAL 3 DAY)) union all select DATE(DATE_SUB( NOW(), INTERVAL 4 DAY)) union all select DATE(DATE_SUB( NOW(), INTERVAL 5 DAY)) union all select DATE(DATE_SUB( NOW(), INTERVAL 6 DAY))) d left outer join transactions t on t.purchase_date = d.thedate and vendor_id = 0 GROUP BY d.thedate ORDER BY d.thedate DESC; 
  • 在控制台中禁用Rails SQL日志logging
  • MySQL查询select上周的数据?
  • SQL统计所有行而不是统计单个行
  • 如何find什么是locking我的表
  • 如果任何字段包含NULL,MySQL CONCAT将返回NULL
  • 什么是SELF JOIN,什么时候使用它?
  • 如何在PostgreSQL中的函数内返回SELECT的结果?
  • Azure SQL数据库Web与商业版
  • 显式与隐式SQL连接
  • 将列更改为可空
  • PostgreSQL:哪种数据types应该用于货币?