简体   繁体   English

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

[英]Count rows until value in column changes in MySQL

I have same problem like in this question Count rows until value in column changes mysql But although the issue was resolved I not understand this query. 我有这个问题一样的问题计数行直到列中的值更改mysql为止尽管解决了问题,但我不理解此查询。 Because I have low reputation points I can't leave comment there and must open new question. 因为我的信誉度很低,所以我不能在此处发表评论,而必须提出新的问题。

My example: 我的例子:

mysql> select * from example;
+----+-------+------+
| id | name  | succ |
+----+-------+------+
|  1 | peter |    1 |
|  2 | bob   |    1 |
|  3 | peter |    0 |
|  4 | peter |    0 |
|  5 | nick  |    1 |
|  6 | bob   |    0 |
|  7 | peter |    1 |
|  8 | bob   |    0 |
|  9 | peter |    1 |
| 10 | peter |    1 |
+----+-------+------+
10 rows in set (0.00 sec)

I want to count successive true values for peter (descending id, and results must be 3), I know how to set query like this : 我想计算彼得的连续真实值(降序编号,结果必须为3),我知道如何设置查询:

mysql> select count(succ) 
       from example 
       where id > (select max(id) from example where succ = 0);
+-------------+
| count(succ) |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

But how to get results just for peter, and if it is possible to get results grouped by name, like this: 但是如何只为彼得获得结果,以及是否有可能按名称对结果进行分组,如下所示:

    +--------+------+
    |name    | succ |
    +--------+------+
    |  peter | 3    |
    |  bob   | 0    |
    |  nick  | 1    |
    +--------+------+

One way to solve this is with a self join. 解决此问题的一种方法是使用自我联接。 Using an outer join you exclude the rows that have a matching row with a higher id value and succ = 0 , and then count the rows with succ = 1 using SUM() and CASE . 使用外部联接,您可以排除具有匹配行的ID值较高且succ = 0的行,然后使用SUM()CASEsucc = 1的行进行计数。

Here's the query for your example: 这是您的示例查询:

select e1.name, 
  sum(case when e1.succ = 1 then 1 else 0 end) as succ
from example e1 
left outer join example e2 on e2.id > e1.id 
  and e2.name = e1.name 
  and e2.succ = 0
where e2.id is null
group by e1.name

Use variables to count consecutive successes (re-starting when seeing a failure), and join with a query which selects highest id per name (somewhat similar to McNets's answer) 使用变量来计算连续的成功次数(看到失败时重新启动),并加入一个查询,该查询选择每个名称的最高ID(有点类似于McNets的答案)

SELECT a.name, a.count FROM (    
  SELECT  e1.id, e1.name, e1.succ,
    @count_success := IF (@prev_name = e1.name AND e1.succ = 1, @count_success + 1, e1.succ) AS `count`,
    @prev_name := e1.name AS `prev_name`
  FROM `example` e1, (SELECT @count_success :=0, @prev_name := NULL) init
  ORDER BY e1.name, e1.id ) `a` 
  JOIN (SELECT MAX(id) AS `max` FROM `example` GROUP BY `name`) `b` ON a.id = b.max

if you need the count of records 如果您需要记录数量

select name, count(succ) from example group by name

or if you need the sum of the succ of every person you can use 或者如果您需要每个人的成功总和,则可以使用

select name, sum(succ) from example group by name 

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

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