简体   繁体   English

计算最后一行,直到列值等于myql中的给定值

[英]Count last rows until column value is equal to given value in myql

Suppose My table has only two column; 假设我的表只有两列; among them one is AUTO_INCREMENT and another contains under line data... 其中一个是AUTO_INCREMENT ,另一个包含行下数据...

A
B
C
A
A

I want to count last rows until desire given value found. 我想计算最后一行,直到发现期望的给定值。

Suppose For, 假设

A : 0 (Count From last to reach A)
C : 2 (Count From last to reach C)
B : 3 (Count From last to reach B)

Get the highest ID for each item, and then count the number of rows that have higher IDs. 获取每个项目的最高ID,然后计算具有较高ID的行数。

SELECT x.item, IFNULL(COUNT(y.id), 0) as count
FROM (SELECT item, MAX(id) AS lastid
      FROM yourTable
      GROUP BY item) AS x
LEFT JOIN yourTable AS y ON y.id > x.lastid
GROUP BY x.item

DEMO 演示

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

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