更新时间:2023-02-12 10:07:05
MySQL user defined variables
在这种情况下会为您提供帮助.
MySQL user defined variables
would help you in this case.
每次看到新状态时,将1
作为行号分配给相应的行.
Every time you see a new status assign 1
as row number to the corresponding row.
如果您看到的状态与上一行相同,请分配一个递增的行号.
And if you see the same status that you saw in the previous row then assign an incremented row number instead.
这样,您最终可以过滤仅具有row number = 1
的记录.这些特定记录实际上与前一行相比显示出差异
This way you can finally filter the records having row number = 1
only. These particular records are actually showing difference comparing to their immediate previous row
SELECT
*
FROM
(
SELECT
*,
IF(@prevStatus = YT.status_1, @rn := @rn + 1,
IF(@prevStatus := YT.status_1, @rn := 1, @rn := 1)
) AS rn
FROM your_table YT
CROSS JOIN
(
SELECT @prevStatus := -1, @rn := 1
) AS var
ORDER BY YT.ID
) AS t
WHERE t.rn = 1
ORDER BY t.ID