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.