197. 上升的温度

表 Weather

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
id 是这个表的主键
该表包含特定日期的温度信息
 

编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id 。

返回结果 不要求顺序 。

查询结果格式如下例:

Weather
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+----+------------+-------------+

Result table:
+----+
| id |
+----+
| 2 |
| 4 |
+----+
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(30 -> 20)

 

select b.id as "IDID" from  Weather a,Weather b where 
a.Temperature <b.Temperature and a.id +1 = b.id  #错的
#id虽是递增,但是表中的数据对应的日期可能是混乱的


datediff(日期1, 日期2): 得到的结果是日期1与日期2相差的天数。 如果日期1比日期2大,结果为正;如果日期1比日期2小,结果为负。

# Write your MySQL query statement below
select b.id  as "Id" from  Weather a,Weather b where 
a.Temperature <b.Temperature and dateDiff(a.RecordDate,b.RecordDate) = -1

或者是下面的写法:

# Write your MySQL query statement below
select a.id  as "Id" from  Weather a,Weather b where 
a.Temperature >b.Temperature and dateDiff(a.RecordDate,b.RecordDate) = 1 #a日期大于b的日期

 

posted @ 2020-10-29 15:57  guofen3399  阅读(61)  评论(0)    收藏  举报