【leetcode】上升的温度Mysql

表 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)

代码:

# Write your MySQL query statement below
select w1.id from Weather w1,Weather w2 where(TO_DAYS(w1.recordDate)-TO_DAYS(w2.recordDate)=1 and w1.Temperature>w2.Temperature)

注意:Day()获取date型数据的日,但不能直接做加减法。需要使用DataDiff(date1,date2)=1或者TO_DAYS(date1)-TO_DAYS(date2)=1判断天数是否相差1。

猜你喜欢

转载自blog.csdn.net/qq_40315080/article/details/116014182
今日推荐