PHP-SQL query rise in temperature

Weather given a table, write a SQL query, to find higher before the date (yesterday) compared to the temperature Id all dates.

------------------ ------------------ + --------- + + +
| the above mentioned id (INT) | RecordDate (DATE) | Temperature (INT) |
+ --------- + ------------------ + ----- + -------------
|. 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
|. 3 | 2015-01-03 | 20 is |
|. 4 | 2015-01-04 | 30 |
+ --------- + ------------ + ------------------ + ------
for example, according to the above given tables Weather, returns the following Id:

+----+
| Id |
+----+
| 2 |
| 4 |
+----+

Source: stay button (LeetCode)

Subqueries

SELECT
    Id 
FROM
    Weather as a 
    where  Temperature > (select Temperature from  Weather as w where DATEDIFF(a.RecordDate, w.RecordDate) = 1
        AND a.Temperature > w.Temperature) order by a.RecordDate desc
 
JOIN query
SELECT
    weather.id AS 'Id'
FROM
    weather
        JOIN
    weather w ON DATEDIFF(weather.RecordDate, w.RecordDate) = 1
        AND weather.Temperature > w.Temperature
 
 
JOIN query execution time can be seen better than subqueries 

Guess you like

Origin www.cnblogs.com/corvus/p/11992629.html