力扣数据库:上升的温度

上升的温度

表 Weather

Column NameType
idint
recordDatedate
temperatureint

id 是这个表的主键
该表包含特定日期的温度信息

编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id 。
返回结果 不要求顺序 。

查询结果格式如下例:
Weather

idrecordDateTemperature
12015-01-0110
22015-01-0225
32015-01-0320
42015-01-0430

Result table:

id
2
4

2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(30 -> 20)

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rising-temperature

思路
使用datediff时间函数,计算时间差,然后采用自连接的非等值连接 约束温度即可。类似的时间函数还有timestampdiff 和 date_sub(curdate(),interval 1 day)
函数的使用方法:
在这里插入图片描述

题解

 select w1.id 
 from Weather as w1
 inner join Weather as w2
 on w1.Temperature > w2.Temperature 
 and datediff(w1.recordDate,w2.recordDate)=1;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值