2013-03-08 294 views
1

我有一个非常简单的表:如何从单个表中选择当前日期和前一日期?

mysql> desc stats; 
+-------------+-------------+------+-----+---------+----------------+ 
| Field  | Type  | Null | Key | Default | Extra   | 
+-------------+-------------+------+-----+---------+----------------+ 
| entry_id | int(11)  | NO | PRI | NULL | auto_increment | 
| entry_date | date  | NO |  | NULL |    | 
| show_name | varchar(40) | NO |  | NULL |    | 
| month_total | int(11)  | NO |  | NULL |    | 
+-------------+-------------+------+-----+---------+----------------+ 

我想一个select语句,这将使我从当前的日期,以及在单个语句前一天的数据结果,因此,例如,结果将有点像

show_name month_total(Current day) monthly_total(Previous Day) 

是否有一种简单的方法可以在单个sql语句中执行此操作?

回答

0

更新:这是有道理的ENTRY_DATE添加到输出

SELECT c.show_name, 
     c.month_total current_month_total, 
     p.month_total prev_month_total 
    FROM stats c INNER JOIN 
     stats p ON p.entry_date = c.entry_date - INTERVAL 1 DAY 

假设你有一个像

+----------+------------+-----------+-------------+ 
| entry_id | entry_date | show_name | month_total | 
+----------+------------+-----------+-------------+ 
|  1 | 2013-03-07 | Name1  |   10 | 
|  2 | 2013-03-08 | Name2  |   20 | 
|  3 | 2013-03-09 | Name3  |   30 | 
|  4 | 2013-03-10 | Name4  |   40 | 
+----------+------------+-----------+-------------+ 

样本数据的查询的输出是

+-----------+------------+---------------------+------------------+ 
| show_name | entry_date | current_month_total | prev_month_total | 
+-----------+------------+---------------------+------------------+ 
| Name2  | 2013-03-08 |     20 |    10 | 
| Name3  | 2013-03-09 |     30 |    20 | 
| Name4  | 2013-03-10 |     40 |    30 | 
+-----------+------------+---------------------+------------------+ 

这是sqlfiddle example

如果您需要特定日期或日期间隔的输出,只需添加WHERE子句即可。
对于今天

... 
WHERE c.entry_date = CURDATE(); 

从03/09到03/10

... 
WHERE c.entry_date BETWEEN '2013-03-09' AND '2013-03-10' 
+0

不应该是相反的方式吗?我的意思是你的前一天实际上是第二天,不是吗? – jurgenreza 2013-03-08 21:33:08

+0

这是表别名中的拼写错误。看到更正的答案 – peterm 2013-03-08 21:34:08

+0

@xxedgexx你需要更多的帮助来解决你的问题吗? – peterm 2013-03-09 02:07:49

0

如果当天你的意思是今天,使用SUBDATE和CURDATE功能,在你的SELECT语句的子查询:

​​

参见fiddle

如果你想每天都这样,而不仅仅是当天,那就去加入吧。

0

假设本次测试数据:

+----------+------------+-----------+-------------+ 
| entry_id | entry_date | show_name | month_total | 
| 1  | 2013-03-07 | test1  |   1 | 
| 2  | 2013-03-07 | test2  |   11 | 
| 3  | 2013-03-08 | test1  |   2 | 
| 4  | 2013-03-08 | test2  |   22 | 
| 5  | 2013-03-08 | test3  |   222 | 
| 6  | 2013-03-09 | test1  |   3 | 
| 7  | 2013-03-09 | test2  |   33 | 
| 8  | 2013-03-07 | test1  |   5 | 
+----------+------------+-----------+-------------+ 

如果你每天多个条目的不同名称,而不是这应该很好地工作:

SELECT c.show_name, c.entry_date, 
     c.month_total current_day_month_total, 
     IFNULL(p.month_total,0) previous_day_month_total 
    FROM test.stats c LEFT JOIN test.stats p 
     ON p.entry_date = c.entry_date - INTERVAL 1 DAY 
      AND c.show_name = p.show_name 
    GROUP BY c.entry_date, c.show_name; 

+-----------+------------+-------------------------+--------------------------+ 
| show_name | entry_date | current_day_month_total | previous_day_month_total | 
| test1  | 2013-03-07 |      1 |      0 | 
| test2  | 2013-03-07 |      11 |      0 | 
| test1  | 2013-03-08 |      2 |      1 | 
| test2  | 2013-03-08 |      22 |      11 | 
| test3  | 2013-03-08 |      222 |      0 | 
| test1  | 2013-03-09 |      3 |      2 | 
| test2  | 2013-03-09 |      33 |      22 | 
+-----------+------------+-------------------------+--------------------------+ 

如果有即使每天和名称多次输入也会结合它们的值: (参见结果表f的第一行)或测试1 - id1:1 + id8:5 = 6)

SELECT c.show_name, c.entry_date, 
     c.month_total current_day_month_total, 
     IFNULL(p.month_total,0) previous_day_month_total 
    FROM (SELECT show_name, entry_date, SUM(month_total) month_total 
     FROM test.stats 
     GROUP BY entry_date, show_name) c 
    LEFT JOIN 
     (SELECT show_name, entry_date, SUM(month_total) month_total 
     FROM test.stats 
     GROUP BY entry_date, show_name) p 
    ON p.entry_date = c.entry_date - INTERVAL 1 DAY 
    AND c.show_name = p.show_name; 

+-----------+------------+-------------------------+--------------------------+ 
| show_name | entry_date | current_day_month_total | previous_day_month_total | 
| test1  | 2013-03-07 |      6 |      0 | 
| test2  | 2013-03-07 |      11 |      0 | 
| test1  | 2013-03-08 |      2 |      6 | 
| test2  | 2013-03-08 |      22 |      11 | 
| test3  | 2013-03-08 |      222 |      0 | 
| test1  | 2013-03-09 |      3 |      2 | 
| test2  | 2013-03-09 |      33 |      22 | 
+-----------+------------+-------------------------+--------------------------+ 
相关问题