简体   繁体   English

计算行数,直到列中的值更改 mysql

[英]Count rows until value in column changes mysql

So I have a table with a column that has a true or false (0 or 1) value.所以我有一个表,其中一列具有真或假(0 或 1)值。 I want to know the count of consecutive 0s before I hit a 1. So for example if I had the following time ASC:我想知道在我击中 1 之前连续 0 的计数。例如,如果我有以下时间 ASC:

0
0
1
0
1
0
0
0

The return number of last consecutive 0s would be 3. I've been looking for a way to count until the value changes but so far no luck.最后一个连续 0 的返回数将是 3。我一直在寻找一种计数方法,直到值发生变化,但到目前为止还没有运气。 Any thoughts?有什么想法吗?

EDIT:编辑:

here's a clearer example of my table这是我的桌子的一个更清晰的例子

|id|counter|user_id|
--------------------
|0 |0      |3      | 
--------------------
|1 |0      |3      | 
--------------------
|2 |1      |3      | 
--------------------
|3 |0      |3      | 
--------------------
|4 |1      |3      | 
--------------------
|5 |0      |3      | 
--------------------
|6 |1      |8      | 
--------------------
|7 |0      |3      | 
--------------------
|8 |0      |3      | 
--------------------

for user_id = 3, the result would be 3对于 user_id = 3,结果将为 3

Your time column seems to specify the ordering.您的time列似乎指定了顺序。 Then the number of zeros at the end would be:那么最后的零数将是:

select count(*)
from table t
where t.col = 0 and
      t.time > (select max(t2.time) from table t2 where t2.col = 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM