如何在mysql中显示连续日期,如果表中没有日期

时间:2017-12-06 04:13:24

标签: php mysql date

这是我需要从db中获取连续数据的保留查询我已经用两个表写了这个查询

  1. deviceinfodatas - 具有id,created_at time
  2. sessions - deviceid,start_time

    选择

    u.DayOffset as Date,

    SUM(s.DayOffset = 0)AS d0,

    SUM(s.DayOffset = 1)AS d1,

    SUM(s.DayOffset = 2)AS d2,

    SUM(s.DayOffset = 3)AS d3

    FROM

    (SELECT id,DATE(created_at)AS DayOffset FROM deviceinfodatas WHERE created_at BETWEEN'2017-10-01'''2017-12-30')as u LEFT JOIN

    (SELECT DISTINCT sessions.deviceid, DATEDIFF(DATE(sessions.start_time),DATE(deviceinfodatas.created_at)) AS DayOffset FROM sessions

    LEFT JOIN deviceinfodatas ON(deviceinfodatas.id = sessions.deviceid)WHERE sessions.start_time BETWEEN'2017-10-01'和'2017-12-30') as s ON s.deviceid = u.id GROUP BY u.DayOffset

  3. 我的结果是

    Date          d0  d1  d2  d3
    
    2017-10-25    1   0   0   0
    
    2017-10-27    1   0   0   0
    
    2017-10-31    0   0   1   1
    
    2017-11-02    1   0   0   0
    
    2017-11-03    1   0   0   0
    
    2017-11-06    1   0   0   0
    
    2017-11-08    1   0   0   0
    
    2017-11-09    0   0   1   0
    
    2017-11-13    0   0   1   0
    
    2017-11-16    5   1   0   0
    

    但我需要一个连续的约会

    as

     Date          d0  d1  d2  d3
    
    2017-10-25    1   0   0   0
    
    2017-10-26    0   0   0   0
    
    2017-10-27    1   0   0   0
    
    2017-10-28    0   0   0   0
    
    2017-10-29    0   0   0   0
    
    2017-10-30    0   0   0   0
    
    2017-10-31    0   0   1   1
    
    .....
    
    2017-11-16    5   1   0   0
    

    并且还根据日期范围计算日d0,d1,d2,d3,d4,d5 ...... dn必须动态增加

    我是Mysql的初学者,请帮助这个查询来实现我的结果

    我引用了Continuous dateDaily retention query

    提前谢谢

2 个答案:

答案 0 :(得分:0)

如果要返回日期值,则查询将需要日期值的行源。这可以是填充了您需要返回的所有日期值的日历表cal,也可以是动态返回日期集的内联视图。

包含唯一值的简单日历表的示例。这个表需要维护,我们需要确保它包含我们想要返回的所有可能的日期值,没有间隙。

CREATE TABLE cal (dt DATE NOT NULL PRIMARY KEY) ENGINE=InnoDB;

INSERT INTO cal (dt) VALUES ('2017-10-01'),('2017-10-02'),('2017-10-03') ;
INSERT INTO cal (dt) SELECT dt + INTERVAL 3 DAY FROM cal ORDER BY dt ;
INSERT INTO cal (dt) SELECT dt + INTERVAL 6 DAY FROM cal ORDER BY dt ;
INSERT INTO cal (dt) SELECT dt + INTERVAL 12 DAY FROM cal ORDER BY dt ;
INSERT INTO cal (dt) SELECT dt + INTERVAL 24 DAY FROM cal ORDER BY dt ;
INSERT INTO cal (dt) SELECT dt + INTERVAL 48 DAY FROM cal ORDER BY dt ; 
INSERT INTO cal (dt) SELECT dt + INTERVAL 96 DAY FROM cal ORDER BY dt ;

然后我们可以这样做:

SELECT c.dt 
     , ...
  FROM cal c

  LEFT
  JOIN mytable t
    ON t.date_col = c.dt 

WHERE c.dt >= '2017-10-25'
  AND c.dt <  '2017-10-25' + INTERVAL 30 DAY

答案 1 :(得分:0)

这是工作查询

如果不创建单独的表格,很容易获得连续日期

这可能有助于某人!

UIBezierPath *maskPath = [UIBezierPath
                          bezierPathWithRoundedRect:ViewSwapMyWork.bounds
                          byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                          cornerRadii:CGSizeMake(70, 70)
                          ];

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = ViewSwapMyWork.bounds;
maskLayer.path = maskPath.CGPath;

ViewSwapMyWork.layer.mask = maskLayer; 

结果如下

SELECT u.DayOffset as Date,
COALESCE(SUM(s.DayOffset = 0),0) AS d0,
COALESCE(SUM(s.DayOffset = 1),0) AS d1,
COALESCE(SUM(s.DayOffset = 2),0) AS d2,
COALESCE(SUM(s.DayOffset = 3),0) AS d3,
COALESCE(SUM(s.DayOffset = 4),0) AS d4,
COALESCE(SUM(s.DayOffset = 5),0) AS d5,
COALESCE(SUM(s.DayOffset = 6),0) AS d6,
COALESCE(SUM(s.DayOffset = 7),0) AS d7,
COALESCE(SUM(s.DayOffset = 8),0) AS d8,
COALESCE(SUM(s.DayOffset = 9),0) AS d9
FROM ( SELECT id, DATE(d.created_at) AS DayOffset from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + 
t0.i) `created_at` from
(select 0 i union select 1 union select 2 union select 3 union select 4 
union select 5 union select 6 union select 7 union select 8 union select 9) 
t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 
union 
select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 
union select 5 union select 6 union select 7 union select 8 union select 9) 
t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 
union select 5 union select 6 union select 7 union select 8 union select 9) 
t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 
union select 5 union select 6 union select 7 union select 8 union select 9) 
t4) d
left join deviceinfodatas v on DATE(d.`created_at`) = DATE(v.`created_at`)
where DATE(d.`created_at`) between '2017-11-01' and '2017-11-11') as u
LEFT JOIN ( SELECT DISTINCT sessions.deviceid, 
DATEDIFF(DATE(sessions.start_time), DATE(deviceinfodatas.created_at))
AS DayOffset FROM sessions
LEFT JOIN deviceinfodatas ON (deviceinfodatas.id = sessions.deviceid) WHERE 
sessions.start_time BETWEEN '2017-11-01' AND '2017-11-11' )
as s ON s.deviceid = u.id GROUP BY u.DayOffset 

我可以实现连续约会但仍在努力获得日数d0,d1,...... d12

我正在尝试在php mysql中我在php中尝试使用查询中的日期差异计数,如果有任何提示将被赞赏

谢谢