面试题:升高的温度
难度:容易
给定表Weather,写一段SQL查询所有前一天温度高过当天温度的记录Id。
+---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------+------------------+
例如:以上数据返回:
+----+ | Id | +----+ | 2 | | 4 | +----+
答案:
SELECT a.id
FROM Weather a, Weather b
WHERE a.Temperature>b.Temperature and DATEDIFF(a.Date, b.Date)=1;