【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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章