我想使用sql代码将每分钟的价格差异(Diff)放入mysql表中的空列中。我每分钟都会改变价格。 之前:
Date, Time, Price, Diff
20140101,230300, 1.3761, NULL
20140101,230400, 1.3762, NULL
运行sql代码后:
Date, Time, Price, Diff
20140101,230300, 1.3761, 0.0000
20140101,230400, 1.3762, 0.0001
答案 0 :(得分:2)
可能没有理由把差异放在桌子上。您可以通过简单的自我连接查询获得差异
约:
select ta.date, ta.time, ta.price, diff as ta.price-tb.price
from mytable ta join mytable tb
where tb.time = ta.time-100;
关于将日期时间作为单个实体的评论将使表格的时差计算变得更加容易,因为有像TIMEDIFF这样的函数可以提供帮助。