计算日期范围之间的值

qij5mzcb  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(394)

所以我有点困惑,我知道如何在理论上做到这一点,但我有困难执行它在实践中。
基本上我有一张table和一张table。该表反映了截至目前的状态,修订表反映了该表过去的状态。

id3,    id2,      id1,   title,   timestamp, status, 
56456   229299  4775    x name      1432866912  0   
56456   232054  123859  x name      1434000054  1   
56456   235578  16623   x name      1435213281  1   
56456   237496  139811  x name      1464765447  1   
56456   381557  0       x name      1487642800  1   
56456   616934  186319  x name      1496103368  1   
56456   668046  246292  x name      1505386262  1   
56456   766390  246292  x name      1523273582  1

基本上我想看看历史 live/offline 表中所有项的状态。所以我知道现在的情况是 live ,我知道参赛日期 offline/live 也。
我要做的是计算时间戳之间的实时或脱机日期。介于1->0之间的日期是实时日期。1->之间的日期是实时日期。0->1之间的日期为脱机日期,0->0之间的日期为脱机日期。
因此,理想情况下,我的数据会有一个实时/离线状态,即在这些状态变化之间的每一天。

输出将显示时间戳之间的日期 1432866912 & 1434000054 状态是 Offline 我试着搜索,但没有看到任何相关的东西。
编辑:
@第一行有一个日期的unixtimestamp May 28, 2015 &第二行是日期的时间戳 June 11, 2015. 第一排是 offline 第二排是 live. 所以我基本上希望我的数据是这样的

Date          Status
May 28, 2015  Offline
May 29, 2015  Offline
May 30, 2015  Offline
....
....
June 9, 2015 Offline
June 10, 2015 Offline
June 11, 2015 Live
June 12, 2015 Live

我需要这样做,因为我们的数据库不是每天都存储数据,而是只在数据发生更改时存储数据。

vof42yt1

vof42yt11#

无法检索不在表中的记录。在这种情况下,必须生成日期序列,然后执行交叉检查或左联接等。
看一下生成日期序列
下面的代码使用修订表中的最小和最大日期生成日期列表。对你的修订表做一个交叉检查,然后得到 last seen/found status code 对于当前行日期。
假设你的table Revisionsstatus 以及 timestamp 领域。以下sql代码适用于您:
在这里摆弄

select
  TDates.genDate, -- generated date sequence 
  (select      case when r.status =0 then 'Offline' else 'Live' end
    from       revisions R
    WHERE      date(from_unixtime(R.Timestamp)) <= TDates.genDate
    order by   R.timestamp desc
    limit 1
  ) as genStatus
from 
  (
    SELECT     * 
    FROM
      (select adddate(T4.minDate, t3*1000 + t2*100 + t1*10 + t0) genDate, T4.minDate, T4.maxDate from
        (select 0 t0 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 t1 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 t2 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 t3 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,
        -- using your table get the min date and max date we are going to check. So we generate just the required dates.
        (select date(from_unixtime(min(R.timestamp))) as minDate, date(from_unixtime(max(R.timestamp))) as maxDate from revisions as R  ) T4
      ) T
    where T.genDate <= T.maxDate
  ) As TDates 
order by TDates.genDate

这只是一个概念,非常欢迎您改进性能提示

k5hmc34c

k5hmc34c2#

使用“case”知道它是离线的还是实时的,使用“date\u format”函数以日期显示时间戳。查看此演示:http://sqlfiddle.com/#!2013年9月88281f

select DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%b %e, %Y') AS  `date` , 
  case when status=0 then 'Offline' else 'Live' end as `status`
from yourTbl
order by `timestamp`

相关问题