【LeetCode】197、Rising Temperature

题目要求:Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.

解答:关于时间的Mysql的函数:

1、TO_DAYS(date):给定一个日期data,返回从年份0开始的天数

2、DATEDIFF():返回两个日期之间的天数,左边的减去右边的数值

3、SUBDATE():实现日期的加减;

4、DATE_ADD() 、DATE_SUB():

语法:DATE_SUB(date,INTERVAL expr type),

示例:SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 2 DAY) AS OrderPayDate FROM Orders

注意,必须得有INTERVAL

1)

SELECT w2.Id AS Id
FROM Weather w1, Weather w2 
WHERE TO_DAYS(w1.Date)+1 = TO_DAYS(w2.Date) AND w1.Temperature < w2.Temperature

2)

SELECT w1.Id AS Id
FROM Weather w1, Weather w2 
WHERE DATEDIFF(w1.Date,w2.Date) =1 AND w1.Temperature > w2.Temperature

3)

SELECT w1.Id AS Id
FROM Weather w1, Weather w2 
WHERE SUBDATE(w1.Date,1) = w2.Date AND w1.Temperature > w2.Temperature

4)

SELECT w1.Id AS Id
FROM Weather w1, Weather w2 
WHERE DATE_SUB(w1.Date,INTERVAL 1 DAY) = w2.Date AND w1.Temperature > w2.Temperature



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值