gpt4 book ai didi

mysql - 选择上一行数据作为当前行数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:07 24 4
gpt4 key购买 nike

我的 mysql 数据库中有以下选择:

select t.col1, t.THE_DATE, (select ?? as PREVIOUS_DATE) 
from DUMMY_TABLE t
order by date

我想要实现的是让“PREVIOUS_DATE”包含上一行的“THE_DATE”列的值(如果有的话)。

所以如果 DUMMY_TABLE 有数据:

col1 THE_DATE 
x 10-01-2010
x 10-01-2012
x 10-01-2009

我的选择应该返回

col1  THE_DATE   PREVIOUS_DATE
x 10-01-2009
x 10-01-2010 10-01-2009
x 10-01-2012 10-01-2010

最佳答案

子查询中需要order by子句和limit子句:

select t.col1, t.the_date, 
( select t1.the_date
from dummy_table t1
where t1.col = t.col and
t1.the_date < t.the_date
order by t1.the_date desc
limit 1
) as PREVIOUS_DATE
from dummy_table t
order by the_date;

关于mysql - 选择上一行数据作为当前行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52483024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com