mysql 求当前时间上下相邻两条数据的sql语句怎么写
mysql> set @last_id := -1;
Query OK, 0 rows affected (0.00 sec)
mysql> select id, A, B, result
-> from
-> (
-> select
-> table1.*,
-> @last_id,
-> if(@last_id < 0, null, id - @last_id) as result,
-> @last_id := id
-> from
-> table1
-> ) as tmp
-> ;
+----+------+------+--------+
| id | A | B | result |
+----+------+------+--------+
| 1 | 2 | 1 | NULL |
| 21 | 1 | 1 | 20 |
| 33 | 3 | 2 | 12 |
+----+------+------+--------+
3 rows in set (0.00 sec)