给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。

+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
|       1 |       2015-01-01 |               10 |
|       2 |       2015-01-02 |               25 |
|       3 |       2015-01-03 |               20 |
|       4 |       2015-01-04 |               30 |
+---------+------------------+------------------+
例如,根据上述给定的 Weather 表格,返回如下 Id:

+----+
| Id |
+----+
|  2 |
|  4 |
+----+

SQL代码:

select Weather.Id
from Weather join  Weather w
on DATEDIFF(Weather.RecordDate,w.RecordDate)=1 and Weather.Temperature>w.Temperature

今天学到了一个sql函数datediff(),用于比较date类型的日期,返回值为两个日期相差的天数

举个例子:datediff(data1,date2),若data1在data2后面,则返回一个负数,否则返回一个0或者正数