【LeetCode】Rising Temperature

Rising Temperature 
Total Accepted: 2437 Total Submissions: 8879 My Submissions Question Solution 
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) | Date(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 |
+----+

【解题思路】

比较今天和昨天的温度,并且加上条件限制即可,简单题。

MySql:

1、1399ms

# Write your MySQL query statement below
select w1.Id from Weather w1 join Weather w2 on
date_sub(w1.Date,interval 1 day) = w2.Date 
and w1.Temperature > w2.Temperature;

2、1615ms

# Write your MySQL query statement below
select Id from Weather w1 where Temperature > 
(select Temperature from Weather where Date = date_sub(w1.Date,interval 1 day));



你可能感兴趣的:(【LeetCode】Rising Temperature)