LeetCode: 197. Rising Temperature
题目描述
Given a Weather table, write a SQL query to find all dates’ Ids with higher temperature compared to its previous (yesterday’s) dates.
+---------+------------------+------------------+
| 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 |
+---------+------------------+------------------+
For example, return the following Ids for the above Weather table:
+----+
| Id |
+----+
| 2 |
| 4 |
+----+
解题思路
The MySQL
DATEDIFFfunction calculates the number of days between twoDATE,DATETIME, orTIMESTAMPvalues.
The syntax of the MySQLDATEDIFFfunction is as follows:DATEDIFF(date_expression_1,date_expression_2);
AC 代码
SELECT W1.Id AS Id
FROM Weather AS w1, Weather AS w2
WHERE DATEDIFF(w1.RecordDate, w2.RecordDate)=1 AND w1.Temperature > w2.Temperature

本文详细介绍了如何使用SQL解决LeetCode上的197号问题——Rising Temperature。通过对比日期和温度,筛选出比前一天温度高的记录。利用DATEDIFF函数计算日期差,结合子查询技巧,实现对数据的有效筛选。

235

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



