表 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 的温度比前一天高(20 -> 30)
题目来源:力扣(LeetCode)
链接:力扣
解题思路: 通过DATEDIFF将两个时间间隔相差一天的数据关联在一起 然后比较两者的气温,当日期大的一方气温更高 也就满足了条件
DATEDIFF : 返回两个时间之间相差的天数 若第一个参数比第二个参数的天数大返回正数,若相反则返回负数。
SQL: select w.id from weather as w left join weather as r on DATEDIFF
(w.recordDate,r.recordDate)=1
where w.temperature>r.temperature;