比较mysql中的前一行

时间:2016-05-27 09:47:36

标签: mysql

我有下表,

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //...
        }
        return super.onOptionsItemSelected(item);
    }

标记的行(*)是目标。该行的End_time必须低于下一个Start_time。 * 1 行的End_time大于 * 2 行的Start_time。在这种情况下,我需要 DETELE 那个 * 1 行!..如何使用Mysql查询删除这样的行。

删除行后,我需要这样的表,

  +-----------------------------------------------------------------------+
  | Start_time          | End_time            | Duration  | Event | Agent |
  +-----------------------------------------------------------------------+
  | 2016-05-26 12:50:10 | 2016-05-26 12:52:54 | 00:02:44  | break | user1 |
  | 2016-05-26 12:52:55 | 2016-05-26 12:56:45 | 00:03:50  | break | user1 |
*1| 2016-05-26 12:56:49 | 2016-05-26 13:27:51 | 00:31:02  | break | user1 |
*2| 2016-05-26 12:56:53 | 2016-05-26 13:13:11 | 00:16:18  | break | user1 |
  | 2016-05-26 13:13:12 | 2016-05-26 13:25:25 | 00:12:13  | break | user1 |
  | 2016-05-26 13:25:26 | 2016-05-26 13:26:23 | 00:00:57  | break | user1 |
  | 2016-05-26 13:26:24 | 2016-05-26 13:27:37 | 00:01:13  | break | user1 |
  | 2016-05-26 14:26:50 | 2016-05-26 14:30:57 | 00:04:07  | break | user1 |
  | 2016-05-26 14:30:58 | 2016-05-26 14:46:08 | 00:15:10  | break | user1 |

1 个答案:

答案 0 :(得分:0)

试试这个:(假设Start_time是顺序且唯一的)将Start_time替换为ID(如果存在)。

delete * from atable where Start_time in ( select b.Start_time
from atable a 
inner join atable b 
on a.Agent=b.Agent 
and a.Event=b.Event 
and a.Start_Time > b.Start_Time
Where b.End_time > a.Start_Time