2

如果我有一个日期列,例如“2013-05-05”,我如何选择上一条记录?我试过了

SELECT DATE, percent_change
FROM aa
WHERE DATE >2012 -12 -31
ORDER BY DATE DESC 
LIMIT 1
4

2 回答 2

1

假设日期是唯一键:

SELECT `date`, percentage_change FROM aa WHERE `date`>'2012-12-31' ORDER BY `date` DESC LIMIT 1;

然后,获取上一条记录:

SELECT `date`, percentage_change FROM aa WHERE `date`>'2012-12-31' ORDER BY `date` DESC LIMIT 1 OFFSET 1;

以及之前的记录:

SELECT `date`, percentage_change FROM aa WHERE `date`>'2012-12-31' ORDER BY `date` DESC LIMIT 1 OFFSET 2;

等等

于 2013-06-09T03:54:47.953 回答
0

要在给定日期之前获取记录,这应该有效:

SELECT DATE, percent_change 
FROM aa 
WHERE DATE < '2013-05-05' 
ORDER BY DATE DESC
LIMIT 1
于 2013-06-09T03:58:32.490 回答