leetcode197. 上升的温度(SQL)必会的

首先必知的:

定义和用法

DATEDIFF() 函数返回两个日期之间的时间。

语法

DATEDIFF(datepart,startdate,enddate)

startdateenddate 参数是合法的日期表达式。

datepart 参数可以是下列的值:

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

实例

例子 1

使用如下 SELECT 语句:

SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate

结果:

DiffDate
1

例子 2

使用如下 SELECT 语句:

SELECT DATEDIFF(day,'2008-12-30','2008-12-29') AS DiffDate

结果:

DiffDate
-1

=============================================

问题描述:

给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。

+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
|       1 |       2015-01-01 |               10 |
|       2 |       2015-01-02 |               25 |
|       3 |       2015-01-03 |               20 |
|       4 |       2015-01-04 |               30 |
+---------+------------------+------------------+
例如,根据上述给定的 Weather 表格,返回如下 Id:

+----+
| Id |
+----+
|  2 |
|  4 |
+----+
sql语句,拿去既可以运行:

select w1.Id as'Id'
from weather as w1,weather as w2
where DATEDIFF(w1.RecordDate, w2.RecordDate) = 1 and w1.Temperature>w2.Temperature;

我要刷100道算法题,第73道

猜你喜欢

转载自blog.csdn.net/guoqi_666/article/details/120464649