LeetCode 197. 上升的温度
描述
表: Weather
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
在 SQL 中,id 是该表的主键。
该表包含特定日期的温度信息
找出与之前(昨天的)日期相比温度更高的所有日期的 id 。
返回结果 无顺序要求 。
结果格式如下例子所示。
示例 1:
输入:
Weather 表:
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+----+------------+-------------+
输出:
+----+
| id |
+----+
| 2 |
| 4 |
+----+
解释:
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(20 -> 30)
DATEDIFF() 函数—— 计算时间差
- 作用:返回两个日期之间的时间差
- 语法
DATEDIFF(datepart,startdate,enddate)- datepart : 日期匹配的格式
- startdate: 开始日期
- enddate: 结束日期
datepart | 缩写 | 意义 |
---|---|---|
year | yy 或者 yyyy | 年 |
quarter | qq 或者 q | 季度 |
month | mm 或者 m | 月 |
week | ww 或者 w | 周 |
day | dd 或者d | 日 |
hour | h或者h | 小时 |
minute | mi 或者 n | 分钟 |
second | ss 或者 s | 秒 |
millisecond | ms | 毫秒 |
题解
# 找出与之前(昨天的)日期相比温度更高的所有日期的 id 。
# 返回结果 无顺序要求 。
select a.id
from Weather a, Weather b
where DATEDIFF(a.recordDate,b.recordDate) =1
and a.temperature > b.temperature;