LeetCode force buckle brush title database (197): rising temperature

topic

Weather given a table, write a SQL query, to find higher before the date (yesterday) compared to the temperature Id all dates.
Here Insert Picture Description
For example, according to the table given Weather, returns the following Id:

Here Insert Picture Description

Resolve

Methods: JOIN and DATEDIFF () clause
algorithm

MySQL to compare the values ​​of two types of dates using DATEDIFF.

Therefore, we can combine with the weather by itself, using the DATEDIFF () function.

MySQL

SELECT
    weather.id AS 'Id'
FROM
    weather
        JOIN
    weather w ON DATEDIFF(weather.date, w.date) = 1
        AND weather.Temperature > w.Temperature
;

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

answer

SELECT
    weather.id AS 'Id'
FROM
    weather
        JOIN
    weather w ON DATEDIFF(weather.recorddate, w.recorddate) = 1
        AND weather.Temperature > w.Temperature
;

Published 698 original articles · won praise 929 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104804648
Recommended