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; 
  • MySQL - 行到列
  • 何时使用自动递增的主键,何时不使用?
  • 什么是最好的检查项目是否存在:select计数(ID)或存在(...)?
  • 在C#/ VB.NET中解码T-SQL CAST
  • SQL Server - 创build一个数据库表的副本,并将其放在同一个数据库?
  • SQL Server将string转换为datetime
  • MongoDB如何避免SQL注入混乱?
  • 如何将MyISAM中的所有表转换为InnoDB?
  • Oracle时间戳数据types
  • 我应该在数据库连接string中设置最大池大小吗? 如果我不这样怎么办?
  • 我怎样才能合并两个MySQL表?