PHP-SQL查询上升的温度

我的未来我决定 提交于 2019-12-06 15:21:28

给定一个 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 |
+----+

来源:力扣(LeetCode)

子查询

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查询
SELECT
    weather.id AS 'Id'
FROM
    weather
        JOIN
    weather w ON DATEDIFF(weather.RecordDate, w.RecordDate) = 1
        AND weather.Temperature > w.Temperature
 
 
执行时间可以看出 JOIN查询优于 子查询 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!