Skip to content

Files

Latest commit

c5dbbb6 · Jun 16, 2023

History

History

0197.Rising Temperature

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Jun 16, 2023
Jun 16, 2023
Jun 16, 2023

English Version

题目描述

表: Weather

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id 是这个表的主键
该表包含特定日期的温度信息

 

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

返回结果 不要求顺序

查询结果格式如下例。

 

示例 1:

输入:
Weather 表:
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1  | 2015-01-01 | 10          |
| 2  | 2015-01-02 | 25          |
| 3  | 2015-01-03 | 20          |
| 4  | 2015-01-04 | 30          |
+----+------------+-------------+
输出:
+----+
| id |
+----+
| 2  |
| 4  |
+----+
解释:
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(20 -> 30)

解法

SQL

SELECT w1.Id
FROM
    Weather AS w1,
    Weather AS w2
WHERE
    DATEDIFF(w1.RecordDate, w2.RecordDate) = 1
    AND w1.Temperature > w2.Temperature;
SELECT
	w2.id AS Id
FROM
	weather AS w1
	JOIN weather AS w2 ON DATE_ADD( w1.recordDate, INTERVAL 1 DAY) = w2.recordDate
WHERE
	w1.temperature < w2.temperature