前言
持续更新了
正文
问题来源
本问题来自leetcode上的197题。
问题描述
给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。
数据库表
+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------------+------------------+
查询结果
+----+
| Id |
+----+
| 2 |
| 4 |
+----+
分析:
select Weather.id as 'Id' from Weather join Weather b on datediff(Weather.RecordDate, b.RecordDate) = 1 and Weather.Temperature > b.Temperature;
总结:
勤思考。
结语
不管怎么样好好加油。