简体   繁体   English

获取Mysql的上一个日期

[英]Get the previous date in Mysql

I have a table formatted similar to this: 我有一个类似于以下格式的表:

Date | 日期| ID | ID | Value | 价值| Difference 区别

I need to get the difference between a record's value column, and the previous record's value column based off of the date. 我需要根据日期获取记录的值列和上一个记录的值列之间的差异。

IE IE浏览器

2 days ago | 2天前| cow | 牛| 1 | 1 | Null 空值

Yesterday | 昨天| cow | 牛| 2 | 2 | Null 空值

Today | 今天| cow | 牛| 3 | 3 | Null 空值

Yesterdays difference would be 1, and today's difference would be 1. 昨天的差值为1,今天的差值为1。

basically, I need to get the previous record based off the date, I don't know the interval's between each record. 基本上,我需要根据日期获取先前的记录,我不知道每条记录之间的间隔。 I've been stumped on this for a while. 我已经为此困扰了一段时间。 I am using Mysql, and Python to do the majority of the calculations. 我正在使用Mysql和Python进行大部分计算。

使用SELECT... WHERE date <= NOW() && date >= ( NOW() - 90000 ) (90,000是25小时,您在插入时间上有一点余地),然后取其中的行之间的差蟒蛇。

In Python, or stored procedure, or other language : 使用Python或存储过程或其他语言:

Query Data using your criteria
Fetch first record
Begin Loop
     Get value of previous record by executing another query (select value from table where   date<current_record_date order by date desc limit 1)
     Update current_record set diff=value-previous value
     Fetch next record, exit loop if no more
Iterate Loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM