Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在mysql中获取连续记录_Php_Mysql - Fatal编程技术网

Php 在mysql中获取连续记录

Php 在mysql中获取连续记录,php,mysql,Php,Mysql,我有如下表结构 ID user_id win 1 1 1 2 1 1 3 1 0 4 1 1 5 2 1 6 2 0 7

我有如下表结构

ID           user_id         win 
1              1               1 
2              1               1 
3              1               0 
4              1               1 
5              2               1 
6              2               0 
7              2               0 
8              2               1 
9              1               0 
10             1               1 
11             1               1 
12             1               1 
13             1               1
14             3               1 
我想为mysql中的每个用户获取连续的winswin=1。与用户_id=1一样,它应该返回4record id 10,11,12,13,对于用户_id=2record id=5,它应该返回1

在检索每个用户的记录后,我可以在php中这样做,但我不知道如何使用对mysql的查询


另外,在性能方面,使用php或mysql会更好。任何帮助都将不胜感激。谢谢

内部查询统计每个条纹。外部查询获取每个用户的最大值。查询未经测试,但基于有效的查询

set @user_id = null;
set @streak = 1;

select user_id, max(streak) from (
  SELECT user_id, streak,
    case when @user_id is null OR @user_id != user_id then @streak := 1 else @streak := @streak + 1 end as streak_formula,
    @user_id := user_id,
    @streak as streak
  FROM my_table
) foo
group by user_id

不确定您是否成功地让另一个查询工作,但下面是我的尝试,确切地说是工作——来证明它

set @x=null;
set @y=0;

select sub.user_id as user_id,max(sub.streak) as streak
from
(
select 
case when @x is null then @x:=user_id end,
case 
when win=1 and @x=user_id then @y:=@y+1 
when win=0 and @x=user_id then @y:=0 
when win=1 and @x<>user_id then @y:=1
when win=0 and @x<>user_id then @y:=0
end as streak,
@x:=user_id as user_id
from your_table
) as sub
group by sub.user_id
下面是如何让它在PHP页面上工作,并测试是否得到正确的结果,我还对查询进行了一些优化:

mysql_query("set @y=0");
$query=mysql_query("select sub.user_id as user_id,max(sub.streak) as streak
from
(
select
case 
when win=1 and @x=user_id then @y:=@y+1 
when win=0 and @x=user_id then @y:=0 
when win=1 and @x<>user_id then @y:=1
when win=0 and @x<>user_id then @y:=0
end as streak,
@x:=user_id as user_id
from your_table
) as sub
group by sub.user_id");
while($row=mysql_fetch_assoc($query)){
print_r($row);}

不,因为1,2,4,9不是连续的。用户_id=1在记录_id=3中失败。好的,1,2是连续赢,你想要最长的连胜吗?是的,确实。对不起,我没有提到这个问题。我想要最长的连胜。谢谢。我正在测试。我很快会告诉你的。我无法为mysql运行它。请你解释一下,这样我就可以纠正我做错的事情了。我用我的表名替换了我的表。还有一件事需要改变吗?你有什么错误?my_表是否包含名为user_id的列?您好,在php中使用mysql_查询函数执行此查询不会返回任何内容,而在phpmyadmin上运行良好。有什么建议吗。Thanks@PriteshGupta您好,我已经编辑了我的帖子,其中包括我如何让它在我制作的php页面上工作。@PriteshGupta也在别人说之前:mysql\u query现在贬值了,您应该使用mysqli或PDO,但我已经在mysql\u query中完成了代码,因为我不希望您突然改变一切。但是值得学习的是——我个人推荐。非常感谢你在这里帮助我,并指出我应该使用PDO。谢谢这个查询中的一个问题是,如果用户的记录不是连续的,那么它将给出错误的结果。例如,如果userid=1有两个连续的胜利,那么接下来的2条user_id=2记录,然后如果user_id再次获胜,它将从1开始计数,而它应该从3开始计数。所以我认为我需要对每个用户进行迭代,以获得该用户的连续胜利。在这方面再多提些建议就好了。