我需要有关 Mysql 查询的帮助。我如何 SUM(Value) 只标记 = 0 但不是表中的所有零,但只有最后一个直零。说也许最后 2 个连续零,也许最后 5 个连续零。在示例图片中,最后 3 个零。
样本数据
最佳答案
相当复杂,但我认为它有效并且可能是你的答案
Select sum(`value`) from
(Select
`value`,
@lastval := @lastval + 1 as row_index
from yourtable
cross join (select @lastval := 0) A) B
where B.row_index > (select max(rowindex) from
(Select
`flag`,
@lastval := @lastval + 1 as rowindex
from yourtable
cross join (select @lastval := 0) C) D
where D.flag = 1)
您可以在 SQLfiddle 中优化我的答案
关于mysql - 基于标志的SQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47428308/