从mysql数据库中选择几天内最长的连胜纪录?

时间:2018-11-18 16:07:11

标签: php mysql date

首先,我在Google和Stackoverflow上都发现了很多类似的问题,但是我似乎无法掌握如何正确执行此操作。

我有一个看起来像这样的表:

    id      ms_date
    ------------------
    1     2018-11-18
    2     2018-11-18
    3     2018-11-20
    4     2018-11-22
    5     2018-11-25
    6     2018-11-26
    7     2018-11-26
    8     2018-11-27
    9     2018-11-28
   10     2018-11-29

我需要做的是从该表中获得Days中最长的条纹。

因此在上面的示例中,最长的条纹是4 days

我发现了这个:https://dzone.com/articles/how-to-find-the-longest-consecutive-series-of-even

它解释了他正在尝试做的事情,这与我要实现的目标相似,但是随后的解释/写作如此糟糕,以至于我无法理解。

我还需要找到这些日期之间的GAP,然后重新开始计算连胜。同样,在上面的示例中,由于日期之间存在间隙,因此Current Streak应该为3 Days

我尝试使用上面链接中的代码,但这是基于SQL的,并且在代码中有一些奇怪的WITH词,根本没有任何意义。

是否有使用PHP和Mysql实现此目的的简便方法?

有人可以请教这个问题吗?

谢谢。

编辑:

我似乎无法在我的PHPMYADMIN中找到软件版本,但这是我在“数据库服务器”部分下看到的内容:

Server: Localhost via UNIX socket
Server type: MariaDB
Server connection: SSL is not being used Documentation
Server version: 10.0.37-MariaDB-0+deb8u1 - (Debian)
Protocol version: 10
User: freemind@localhost
Server charset: UTF-8 Unicode (utf8)

第二编辑:

根据以下答案尝试了以下操作,但我一无所获:

$sql_COUNT = "SELECT COUNT(*) max_streak 
  FROM 
     ( SELECT x.*
            , CASE WHEN @prev = val - 1 THEN @i:=@i ELSE @i:=@i+1 END i
            , @prev:=val  
         FROM 
            ( SELECT DISTINCT ms_date FROM MY_TABLE ) x
         JOIN 
            ( SELECT @prev:=null,@i:=0 ) vars 
        ORDER 
           BY ms_date
     ) a 
 GROUP 
    BY i 
 ORDER 
    BY max_streak DESC LIMIT 1";
$query_COUNT = mysqli_query($db_conx, $sql_COUNT);
$productCount_COUNT = mysqli_num_rows($query_COUNT); // count the output amount

echo $productCount_COUNT;

第三次修改:

以下代码根据我在MYSQL数据库中的内容在页面上回显1,但应回显4

$sql_COUNT = "SELECT COUNT(*) AS max_streak 
  FROM 
     ( SELECT x.*
            , CASE WHEN @prev = ms_date - 1 THEN @i:=@i ELSE @i:=@i+1 END i
            , @prev:=ms_date  
         FROM 
            ( SELECT DISTINCT ms_date FROM MY_TABLE ) x
         JOIN 
            ( SELECT @prev:=null,@i:=0 ) vars 
        ORDER 
           BY ms_date
     ) a 
 GROUP 
    BY i 
 ORDER 
    BY max_streak DESC LIMIT 1";
$query_COUNT = mysqli_query($db_conx, $sql_COUNT);

$productCount_COUNT = mysqli_num_rows($query_COUNT); // count the output amount

echo $productCount_COUNT;   

第四次编辑:

我不确定下面的答案是否经过测试,但对我来说不起作用。

我尝试了以下操作,即使没有错误,我也只看到一个空白页,这意味着代码无法正常工作

$sql_COUNT = "SELECT COUNT(*) max_streak 
  FROM 
     ( SELECT x.*
            , CASE WHEN @prev = ms_date - INTERVAL 1 DAY THEN @i:=@i ELSE @i:=@i+1 END i
            , @prev:=ms_date  
         FROM 
            ( SELECT DISTINCT ms_date FROM MY_TABLE ORDER BY ms_date ) x
         JOIN 
            ( SELECT @prev:=null,@i:=0 ) vars 
     ) a 
 GROUP 
    BY i 
 ORDER 
    BY max_streak DESC";
$query_COUNT = mysqli_query($db_conx, $sql_COUNT);

$count = mysqli_fetch_array($query_COUNT);
echo $count[0];

1 个答案:

答案 0 :(得分:2)

请考虑以下内容...

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,val INT NOT NULL
);

INSERT INTO my_table VALUES
(1   , 11),
(2   , 11),
(3   , 12),
(4   , 13),
(5   , 14),
(6   , 17),
(7   , 18),
(8   , 20),
(9   , 21),
(10  , 22);

SELECT COUNT(*) max_streak 
  FROM 
     ( SELECT x.*
            , CASE WHEN @prev = val - 1 THEN @i:=@i ELSE @i:=@i+1 END i
            , @prev:=val  
         FROM 
            ( SELECT DISTINCT val FROM my_table ) x
         JOIN 
            ( SELECT @prev:=null,@i:=0 ) vars 
        ORDER 
           BY val
     ) a 
 GROUP 
    BY i 
 ORDER 
    BY max_streak DESC LIMIT 1;
+------------+
| max_streak |
+------------+
|          4 |
+------------+
1 row in set (0.01 sec)

编辑:

如果您使用日期,那么逻辑是一样的,但是您只需要替换一些日期算术...

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,val DATE NOT NULL
);

INSERT INTO my_table VALUES
(1   , '2018-11-11'),
(2   , '2018-11-11'),
(3   , '2018-11-12'),
(4   , '2018-11-13'),
(5   , '2018-11-14'),
(6   , '2018-11-17'),
(7   , '2018-11-18'),
(8   , '2018-11-20'),
(9   , '2018-11-21'),
(10  , '2018-11-22');

SELECT COUNT(*) max_streak 
  FROM 
     ( SELECT x.*
            , CASE WHEN @prev = val - INTERVAL 1 DAY THEN @i:=@i ELSE @i:=@i+1 END i
            , @prev:=val  
         FROM 
            ( SELECT DISTINCT val FROM my_table ) x
         JOIN 
            ( SELECT @prev:=null,@i:=0 ) vars 
        ORDER 
           BY val
     ) a 
 GROUP 
    BY i 
 ORDER 
    BY max_streak DESC LIMIT 1;
+------------+
| max_streak |
+------------+
|          4 |
+------------+

纯粹主义者会争论(正确)我使用@variable分配的方式不正确-但这只是我养成的不良习惯。也就是说,我认为正确的分配方法如下,但是毫无疑问,如果我弄错了,有人会告诉我...

SELECT COUNT(*) max_streak 
  FROM 
     ( SELECT x.*
            , CASE WHEN @prev = val - INTERVAL 1 DAY THEN @i:=@i ELSE @i:=@i+1 END i
            , @prev:=val  
         FROM 
            ( SELECT DISTINCT val FROM my_table ORDER BY val ) x
         JOIN 
            ( SELECT @prev:=null,@i:=0 ) vars 
     ) a 
 GROUP 
    BY i 
 ORDER 
    BY max_streak DESC LIMIT 1;

如果您想将其捆绑到一些PHP中,我想它可能看起来像这样(尽管从下面的内容中可以明显看出应用程序代码并不是我的强项)...

<?php

require('path/to/connection/stateme.nts');

$query = "
SELECT COUNT(*) max_streak
  FROM
     ( SELECT x.*
            , CASE WHEN @prev = val - INTERVAL 1 DAY THEN @i:=@i ELSE @i:=@i+1 END i
            , @prev:=val
         FROM
            ( SELECT DISTINCT val FROM MY_TABLE ORDER BY val ) x
         JOIN
            ( SELECT @prev:=null,@i:=0 ) vars
     ) a
 GROUP
    BY i
 ORDER
    BY max_streak DESC
 LIMIT 1;
";

$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

$row = mysqli_fetch_assoc($result);

print_r($row);

?>

-- outputs

Array ( [max_streak] => 4 )