上升的温度

上升的温度

题目:

表 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') 结果为指定日期格式;
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 ;

阅读原文

posted @ 2020-11-22 11:16  无尽白日梦  阅读(81)  评论(0)    收藏  举报