Mysql lead 下一条 ; lag 上一条;用法
Posted 二十六画生的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql lead 下一条 ; lag 上一条;用法相关的知识,希望对你有一定的参考价值。
mysql> select * from exam_record ;
+----+------+---------+---------------------+---------------------+-------+
| id | uid | exam_id | start_time | submit_time | score |
+----+------+---------+---------------------+---------------------+-------+
| 1 | 1006 | 9003 | 2021-09-06 10:01:01 | 2021-09-06 10:21:02 | 84 |
| 2 | 1006 | 9001 | 2021-08-02 12:11:01 | 2021-08-02 12:31:01 | 89 |
| 3 | 1006 | 9002 | 2021-06-06 10:01:01 | 2021-06-06 10:21:01 | 81 |
| 4 | 1006 | 9002 | 2021-05-06 10:01:01 | 2021-05-06 10:21:01 | 81 |
| 5 | 1006 | 9001 | 2021-05-01 12:01:01 | NULL | NULL |
| 6 | 1001 | 9001 | 2021-09-05 10:31:01 | 2021-09-05 10:51:01 | 81 |
| 7 | 1001 | 9003 | 2021-08-01 09:01:01 | 2021-08-01 09:51:11 | 78 |
| 8 | 1001 | 9002 | 2021-07-01 09:01:01 | 2021-07-01 09:31:00 | 81 |
| 9 | 1001 | 9002 | 2021-07-01 12:01:01 | 2021-07-01 12:31:01 | 81 |
| 10 | 1001 | 9002 | 2021-07-01 12:01:01 | NULL | NULL |
+----+------+---------+---------------------+---------------------+-------+
10 rows in set (0.01 sec)
mysql> select *,
-> lead(start_time, 1) over(partition by uid order by start_time asc) as next_start_time -- lead 往后查询
-> from exam_record
-> ;
+----+------+---------+---------------------+---------------------+-------+---------------------+
| id | uid | exam_id | start_time | submit_time | score | next_start_time |
+----+------+---------+---------------------+---------------------+-------+---------------------+
| 8 | 1001 | 9002 | 2021-07-01 09:01:01 | 2021-07-01 09:31:00 | 81 | 2021-07-01 12:01:01 |
| 9 | 1001 | 9002 | 2021-07-01 12:01:01 | 2021-07-01 12:31:01 | 81 | 2021-07-01 12:01:01 |
| 10 | 1001 | 9002 | 2021-07-01 12:01:01 | NULL | NULL | 2021-08-01 09:01:01 |
| 7 | 1001 | 9003 | 2021-08-01 09:01:01 | 2021-08-01 09:51:11 | 78 | 2021-09-05 10:31:01 |
| 6 | 1001 | 9001 | 2021-09-05 10:31:01 | 2021-09-05 10:51:01 | 81 | NULL |
| 5 | 1006 | 9001 | 2021-05-01 12:01:01 | NULL | NULL | 2021-05-06 10:01:01 |
| 4 | 1006 | 9002 | 2021-05-06 10:01:01 | 2021-05-06 10:21:01 | 81 | 2021-06-06 10:01:01 |
| 3 | 1006 | 9002 | 2021-06-06 10:01:01 | 2021-06-06 10:21:01 | 81 | 2021-08-02 12:11:01 |
| 2 | 1006 | 9001 | 2021-08-02 12:11:01 | 2021-08-02 12:31:01 | 89 | 2021-09-06 10:01:01 |
| 1 | 1006 | 9003 | 2021-09-06 10:01:01 | 2021-09-06 10:21:02 | 84 | NULL |
+----+------+---------+---------------------+---------------------+-------+---------------------+
10 rows in set (0.00 sec)
mysql> select *,
-> lag(start_time, 1) over(partition by uid order by start_time asc) as next_start_time -- lead 往后查询
-> from exam_record
-> ;
+----+------+---------+---------------------+---------------------+-------+---------------------+
| id | uid | exam_id | start_time | submit_time | score | next_start_time |
+----+------+---------+---------------------+---------------------+-------+---------------------+
| 8 | 1001 | 9002 | 2021-07-01 09:01:01 | 2021-07-01 09:31:00 | 81 | NULL |
| 9 | 1001 | 9002 | 2021-07-01 12:01:01 | 2021-07-01 12:31:01 | 81 | 2021-07-01 09:01:01 |
| 10 | 1001 | 9002 | 2021-07-01 12:01:01 | NULL | NULL | 2021-07-01 12:01:01 |
| 7 | 1001 | 9003 | 2021-08-01 09:01:01 | 2021-08-01 09:51:11 | 78 | 2021-07-01 12:01:01 |
| 6 | 1001 | 9001 | 2021-09-05 10:31:01 | 2021-09-05 10:51:01 | 81 | 2021-08-01 09:01:01 |
| 5 | 1006 | 9001 | 2021-05-01 12:01:01 | NULL | NULL | NULL |
| 4 | 1006 | 9002 | 2021-05-06 10:01:01 | 2021-05-06 10:21:01 | 81 | 2021-05-01 12:01:01 |
| 3 | 1006 | 9002 | 2021-06-06 10:01:01 | 2021-06-06 10:21:01 | 81 | 2021-05-06 10:01:01 |
| 2 | 1006 | 9001 | 2021-08-02 12:11:01 | 2021-08-02 12:31:01 | 89 | 2021-06-06 10:01:01 |
| 1 | 1006 | 9003 | 2021-09-06 10:01:01 | 2021-09-06 10:21:02 | 84 | 2021-08-02 12:11:01 |
+----+------+---------+---------------------+---------------------+-------+---------------------+
10 rows in set (0.00 sec)
mysql>
以上是关于Mysql lead 下一条 ; lag 上一条;用法的主要内容,如果未能解决你的问题,请参考以下文章