Leetcode197. 上升的温度
题目:
给定一个 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中的DATEDIFF函数
select DATEDIFF('2007-12-31','2007-12-30')
返回值:1
select t1.Id from
Weather t1
join
Weather t2
where DATEDIFF(t1.RecordDate,t2.RecordDate)=1
and t1.Temperature > t2.Temperature
310

被折叠的 条评论
为什么被折叠?



