Skip to content

Commit 89a210b

Browse files
committed
Add solution 197 [sql]
inner join datediff()
1 parent 6da1e8f commit 89a210b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
4141
| 183 | [Customers Who Never Order](https://github.com/doocs/leetcode/tree/master/solution/183.Customers%20Who%20Never%20Order) | `SQL` |
4242
| 189 | [Rotate Array](https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
4343
| 196 | [Delete Duplicate Emails](https://github.com/doocs/leetcode/tree/master/solution/196.Delete%20Duplicate%20Emails) | `SQL` |
44+
| 197 | [Rising Temperature](https://github.com/doocs/leetcode/tree/master/solution/197.Rising%@0Temperature) | `SQL` |
4445
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
4546
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
4647
| 231 | [Power of Two](https://github.com/doocs/leetcode/tree/master/solution/231.Power%20of%20Two) | `Math`, `Bit Manipulation` |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## 上升的温度
2+
### 题目描述
3+
4+
5+
给定一个 `Weather` 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。
6+
```
7+
+---------+------------------+------------------+
8+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
9+
+---------+------------------+------------------+
10+
| 1 | 2015-01-01 | 10 |
11+
| 2 | 2015-01-02 | 25 |
12+
| 3 | 2015-01-03 | 20 |
13+
| 4 | 2015-01-04 | 30 |
14+
+---------+------------------+------------------+
15+
```
16+
17+
例如,根据上述给定的 `Weather` 表格,返回如下 Id:
18+
```
19+
+----+
20+
| Id |
21+
+----+
22+
| 2 |
23+
| 4 |
24+
+----+
25+
```
26+
27+
### 解法
28+
利用 datediff 函数返回两个日期相差的天数。
29+
30+
```sql
31+
# Write your MySQL query statement below
32+
select a.Id
33+
from Weather a
34+
inner join Weather b
35+
on a.Temperature > b.Temperature and datediff(a.RecordDate, b.RecordDate) = 1;
36+
```
37+
38+
#### Input
39+
```json
40+
{"headers": {"Weather": ["Id", "RecordDate", "Temperature"]}, "rows": {"Weather": [[1, "2015-01-01", 10], [2, "2015-01-02", 25], [3, "2015-01-03", 20], [4, "2015-01-04", 30]]}}
41+
```
42+
43+
#### Output
44+
```json
45+
{"headers":["Id"],"values":[[2],[4]]}
46+
```

0 commit comments

Comments
 (0)