Mysql 查找只能向上更改的字段的错误记录

Mysql 查找只能向上更改的字段的错误记录,mysql,Mysql,我有一张这样的桌子: +--+----------+--------+-----+---+ |id|date |machine |start|end| +--+----------+--------+-----+---+ | 1|2017-05-24|Machine1| 100|109| | 2|2017-05-24|Machine2| 550|560| | 3|2017-05-25|Machine1| 108|116| | 4|2017-05-26|Machine1| 116

我有一张这样的桌子:

+--+----------+--------+-----+---+
|id|date      |machine |start|end|
+--+----------+--------+-----+---+
| 1|2017-05-24|Machine1|  100|109|
| 2|2017-05-24|Machine2|  550|560|
| 3|2017-05-25|Machine1|  108|116|
| 4|2017-05-26|Machine1|  116|124|
| 5|2017-05-26|Machine2|  570|580|
+--+----------+--------+-----+---+
开始和结束字段是每台机器的小时计数器。计数器只能上升。在id为3的行中,Machine1的起始值小于id为1的行中Machine1的结束值


是否有方法查询返回所有有错误的行?

这将按顺序扫描所有行,并对照上一行检查每一行:

SELECT
    t.*,
    IF(machine = @last_machine AND @last_end > `start`, @last_id, null) as wrong_pair_id,
    @last_machine := machine as __t1,
    @last_end := `end` as __t2,
    @last_id := id as __t3
FROM (
    SELECT * FROM tbl ORDER BY machine, `date`
) t
JOIN (SELECT @last_id := null, @last_end:=null, @last_machine:=null) as i
HAVING wrong_pair_id IS NOT NULL

如果要选择行对,您可以将其包装在另一个选择中,并在错误的行对id上进行连接

select x.*从my_table x在y上连接my_table y。first_thing与x相同。first_thing与y。second_thing大于x。second_thing与y。third_thing小于x。third_thing能否发布
show CREATE的输出表格名称
@last\u id:=id作为\uu t3应该在@last\u结束行之后?
是的,我错过了那一行