[英]PHP query difference between 2 rows MySql table
您好,我正在尋找一種使Mysql查詢中2行之間有所不同的方法
datetime | value
2016-01-04 10:00:00 | 50
2016-01-04 11:00:00 | 60
2016-01-04 13:00:00 | 65
我正在尋找的結果是:
datetime | value
2016-01-04 10:00:00 | 0
2016-01-04 11:00:00 | 10
2016-01-04 13:00:00 | 5
請問如何通過php獲取此結果?
一種方法使用相關子查詢。 請注意,這將為第一個差異返回NULL
:
select t.*,
(t.value -
(select t2.value
from t t2
where t2.datetime < t.datetime
order by t2.datetime desc
limit 1
)
) as diff
from t;
將NULL
轉換為0
很容易,但是我更喜歡NULL
值,因為前一個值沒有意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.