- 题目描述:
给出下面一个weather表,写mysql查询找出所有相比于前一天温度高的ID
可以得出下图结果:
- 题目解答:
分析: 在这里,需运用DATEDIFF() 函数返回两个日期之间的天数:
DATEDIFF(date1,date2)
date1 和 date2 参数是合法的日期或日期/时间表达式。
注释:只有值的日期部分参与计算。
例如:SELECT DATEDIFF('2008-11-30','2008-11-29') AS DiffDate
结果为:DiffDate 1
解答:
方法一:
SELECT weather.id AS id
FROM weather JOIN weather w
ON DATEDIFF(weather.date, w.date) = 1
AND weather.Temperature > w.Temperature
方法二:
select a.Id
from Weather a, Weather b
where a.Temperature > b.Temperature
and DATEDIFF(a.Date, b.Date) = 1