上升的温度
表 Weather
Column Name | Type |
---|---|
id | int |
recordDate | date |
temperature | int |
id 是这个表的主键
该表包含特定日期的温度信息
编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id 。
返回结果 不要求顺序 。
查询结果格式如下例:
Weather
id | recordDate | Temperature |
---|---|---|
1 | 2015-01-01 | 10 |
2 | 2015-01-02 | 25 |
3 | 2015-01-03 | 20 |
4 | 2015-01-04 | 30 |
Result table:
id |
---|
2 |
4 |
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(30 -> 20)
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rising-temperature
思路
使用datediff时间函数,计算时间差,然后采用自连接的非等值连接 约束温度即可。类似的时间函数还有timestampdiff 和 date_sub(curdate(),interval 1 day)
函数的使用方法:
题解
select w1.id
from Weather as w1
inner join Weather as w2
on w1.Temperature > w2.Temperature
and datediff(w1.recordDate,w2.recordDate)=1;