mysql datediff 力扣题目

mysql datediff函数使用实例,题目来源力扣


题目:https://leetcode-cn.com/problems/rising-temperature/
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 的温度比前一天高(20 -> 30)

select a.id 
from weather a join weather b on a.temperature>b.temperature and datediff(day,a.recordDate,b.recordDate)=-1;

DATEDIFF(datepart,startdate,enddate) (参数,开始日期,结束日期)
参数来源:https://www.w3school.com.cn/sql/func_datediff.asp

datepart缩写
yy, yyyy
季度qq, q
mm, m
年中的日dy, y
dd, d
wk, ww
星期dw, w
小时hh
分钟mi, n
ss, s
毫秒ms
微妙mcs
纳秒ns

结果应为 结束日期-开始日期
两表连接 如果a表的temperature大于b表的 temperature,那么满足条件的应该是a的日期大于b的日期,b.recordDate-a.recordDate=-1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值