如何获取mysql中的下一行和上一行?

时间:2018-09-14 00:25:13

标签: sql mysql-5.7

我有一个employees表,

id   name   startDate   endDate
1    John   2013-8-9    2020-10-30
5    Nick   2011-6-7    2020-10-2
7    Pick   2012-6-9    2019-10-12

我有按开始日期排序的员工列表。现在单击记录,我还想根据开始日期获得下一个和上一个行。 像上面的示例一样,该数组被排序为1、7和5,所以当我单击7时,我还需要获取前一个1和下一个5。请您帮忙,我得到了id的记录,检查了大于该startDate的{​​{1}}的日期,然后我按id的顺序按ASC顺序排序,并按一个。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用子查询来计算起始日期较低或起始日期相同但ID较低的行数。基于此,您可以通过减去或加1来选择该计数等于具有所需ID的行的计数以及上一行或下一行的计数的行。

SELECT x.id,
       x.name,
       x.startdate,
       x.end
       FROM (SELECT t1.id,
                    t1.name,
                    t1.startdate,
                    t1.enddate,
                    (SELECT count(startdate)
                            FROM elbat t2
                            WHERE t2.startdate < t1.startdate
                                  OR t2.startdate = t1.startdate
                                     AND t2.id < t1.id) rn
                    FROM elbat t1
             UNION ALL
             SELECT NULL,
                    NULL,
                    NULL,
                    NULL,
                    -1
                    FROM dual
                    WHERE (SELECT (SELECT count(startdate)
                                          FROM elbat t2
                                          WHERE t2.startdate < t1.startdate
                                                 OR t2.startdate = t1.startdate
                                                    AND t2.id < t1.id)
                                          FROM elbat t1
                                          WHERE t1.id = ?) = 0
             UNION ALL
             SELECT NULL,
                    NULL,
                    NULL,
                    NULL,
                    (SELECT count(startdate)
                            FROM elbat t1) + 1
                    FROM dual
                    WHERE (SELECT (SELECT count(startdate)
                                          FROM elbat t2
                                          WHERE t2.startdate < t1.startdate
                                                 OR t2.startdate = t1.startdate
                                                    AND t2.id < t1.id)
                                          FROM elbat t1
                                          WHERE t1.id = ?) = (SELECT count(startdate)
                                                                     FROM elbat t1)) x
       WHERE x.rn IN ((SELECT (SELECT count(startdate)
                                      FROM elbat t2
                                      WHERE t2.startdate < t1.startdate
                                            OR t2.startdate = t1.startdate
                                               AND t2.id < t1.id)
                                      FROM elbat t1
                                      WHERE t1.id = ?) - 1,
                      (SELECT (SELECT count(startdate)
                                      FROM elbat t2
                                      WHERE t2.startdate < t1.startdate
                                            OR t2.startdate = t1.startdate
                                               AND t2.id < t1.id)
                                      FROM elbat t1
                                      WHERE t1.id = ?),
                      (SELECT (SELECT count(startdate)
                                      FROM elbat t2
                                      WHERE t2.startdate < t1.startdate
                                            OR t2.startdate = t1.startdate
                                               AND t2.id < t1.id)
                                      FROM elbat t1
                                      WHERE t1.id = ?) + 1)
     ORDER BY x.rn;

未经测试,因为没有提供足够的DDL或DML!

用您要查询的ID替换?。如果不需要或不需要NULL的行,则如果没有上一行或下一行,则删除UNION ALL ed子查询。说起NULL:目前来看,它不适用于开始日期为空的行。