1. 程式人生 > >【leetcode Database】197. Rising Temperature

【leetcode Database】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) | 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 |
+----+
解析:

本題可以用自連線來做,用表的別名來解決即可。程式碼如下:

# Write your MySQL query statement below
SELECT a.Id  FROM Weather a,Weather b WHERE a.Temperature > b.Temperature AND a.Date = ADDDATE(b.Date,1);