【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);



發佈了105 篇原創文章 · 獲贊 77 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章