登录

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

内容来源于 Stack Overflow,遵循 CC BY-SA 4.0 许可协议进行翻译与使用。IT领域专用引擎提供翻译支持

腾讯云小微IT领域专用引擎提供翻译支持

原文
Stack Overflow用户 修改于2018-09-14
  • 该问题已被编辑
  • 提问者: Stack Overflow用户
  • 提问时间: 2018-09-14 08:25

我有一张桌子 employees as,

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 得到了记录,检查了比 id startDate 更晚的日期,然后我按ASC顺序由 statDate 订购,并将记录限制为1。

浏览 294 关注 0 得票数 0
  • 得票数为Stack Overflow原文数据
原文
回答于2018-09-14
得票数 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,
                    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 s:按照目前的情况,它不能在开始日期为空的行上工作。

回答于2018-09-14
得票数 0

对于"previous",您可以使用:

select e.*
from employees e
where e.startDate > (select e2.startDate from employees e2 where e2.id = 7)