上升的温度
上升的温度
题目:
表 Weather:
| Column Name | Type |
|---|---|
| id | int |
| recordDate | date |
| temperature | int |
id 是这个表的主键
该表包含特定日期的温度信息
要求:编写一个 SQL 查询,来查找与前一天相比温度更高的所有日期的 id。
Result table:
| id |
|---|
| 2 |
| 4 |
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(30 -> 20)
题解:
- 将表进行自连接;
- 条件为左表的前一天recorDate字段=右表recordDate字段,且左表的temperature的值>右表的temperature的值;
- 使用函数:
- date_sub(
,interval DAY) 结果为 前 的日期; - date_format(
,'%Y-%m-%d') 结果为指定日期格式;
- date_sub(
select w1.id
from Weather w1 join Weather w2
on date_format(date_sub(w1.recordDate,interval 1 day),'%Y-%m-%d') = date_format(w2.recordDate,'%Y-%m-%d') and w1.temperature > w2.temperature ;
本文来自博客园,作者:无尽白日梦,转载请注明原文链接:https://www.cnblogs.com/endless-daydream/p/14018645.html

浙公网安备 33010602011771号