mysql连续计算天数

irtuqstp  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(304)

我有两张table。两者都包含一个用户的条目,一个来自早晨,一个来自晚上。
我试图得到的天数,用户因此增加了一个条目在上午和晚上。每当有一个失踪,“连胜”结束-结束在当天。因此,如果用户在一年前是活跃的,并且连续有100天,它并不介意,只是“从今天开始,用户连续有多少天在早晚表中添加条目”。
两个表都有每个条目的时间戳。
举个例子:

morningTable:

ID text timestamp
1  bla  2020-08-07 10:30:00
2  blub 2020-08-06 11:30:00
3  abc  2020-08-05 09:20:00
4  def  2020-08-04 06:13:00
5  ghi  2020-08-02 07:05:00

eveningTable:

ID text timestamp
1  abc  2020-08-07 19:30:00
2  def  2020-08-06 20:45:00
3  ghi  2020-08-05 21:30:00
4  jkl  2020-08-03 20:13:00
5  mno  2020-08-01 17:33:00
6  pqr  2020-07-29 19:19:19

所以有了这两个表,结果必须是数字3,因为两个表都有05.08-07.08的一行。。。
我希望你能理解我的情况。
我找到了rownumber的例子来找出差距等,但是,很难将它与两个表结合起来。
有人能帮我怎么用mysql吗?
我还考虑过用php收集所有数据并在那里进行处理,但是,当数据增长时,用mysql(数据库端)进行处理要比用php进行处理好得多。
提前谢谢!

brtdzjyr

brtdzjyr1#

使用组合表格 union all 加起来,两个都有好几天了。然后使用间隙和孤岛方法:

with me as (
      select dte
      from ((select date(timestamp) as dte, 1 as morning, 0 as evening
             from morning
            ) union all
            (select date(timestamp) as dte, 0 as morning, 1 as evening
             from evening
            )
           ) me
      group by dte
      having sum(morning) > 0 and sum(evening) > 0
     )
select min(dte), max(dte), count(*)
from (select dte, row_number() over (order by dte) as seqnum
      from me
     ) me
group by dte - interval seqnum day
order by count(*) desc
limit 1;

这是一把小提琴。
编辑:
实际上,要使变量适应这个问题并不特别困难:

select min(dte), max(dte), count(*)
from (select dte, (@rn := @rn + 1) as seqnum
      from (select dte
            from ((select date(timestamp) as dte, 1 as morning, 0 as evening
                   from morning
                  ) union all
                  (select date(timestamp) as dte, 0 as morning, 1 as evening
                   from evening
                  )
                 ) me
            group by dte
            having sum(morning) > 0 and sum(evening) > 0
            order by dte
           ) d cross join
           (select @rn := 0) params
     ) me
group by dte - interval seqnum day
order by count(*) desc
limit 1;
iaqfqrcu

iaqfqrcu2#

WITH 
cte1 AS ( SELECT DISTINCT DATE(m.`timestamp`) uni_date                              
          FROM morningTable m
          JOIN eveningTable e ON DATE(m.`timestamp`) = DATE(e.`timestamp`) ),
cte2 AS ( SELECT uni_date, ROW_NUMBER() OVER (ORDER BY uni_date DESC) rn
          FROM cte1 )
SELECT MAX(DATEDIFF( CURRENT_DATE, uni_date ) + 1) output
FROM cte2
WHERE rn = DATEDIFF( CURRENT_DATE, uni_date ) + 1

如果表包含太多的行,那么限制签入的日期可能是安全的 cte1 有一些日期值,在后一个与保证的差距之前。
抱歉,这是从10.2.1开始提供的。。。
在10.1.45中,下一个查询必须工作:

SELECT DATEDIFF(CURRENT_DATE, MIN(uni_date)) + 1 output
FROM ( SELECT DISTINCT DATE(m.`timestamp`) uni_date                              
       FROM morningTable m
       JOIN eveningTable e ON DATE(m.`timestamp`) = DATE(e.`timestamp`) ) t1
WHERE NOT EXISTS ( SELECT NULL
                   FROM ( SELECT DISTINCT DATE(m.`timestamp`) uni_date                              
                          FROM morningTable m
                          JOIN eveningTable e ON DATE(m.`timestamp`) = DATE(e.`timestamp`) ) t2
                   WHERE t2.uni_date = t1.uni_date - INTERVAL 1 DAY )
  AND ( SELECT DATE(MAX(m.`timestamp`))
        FROM morningTable m
        JOIN eveningTable e ON DATE(m.`timestamp`) = DATE(e.`timestamp`) ) = CURRENT_DATE

如果今天至少有一个表中没有行,那么查询将返回null—您可以使用coalesce for来获得零。

相关问题