MySQL日期查询语法

时间:2013-06-09 03:51:32

标签: mysql

如果我有日期列,例如“2013-05-05”,如何选择以前的记录?我试过了

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

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

SELECT DATE, percent_change 
FROM aa 
WHERE DATE < '2013-05-05' 
ORDER BY DATE DESC
LIMIT 1