当前位置: 首页> 文旅> 文化 > 力扣高频SQL 50题(基础版)第九题

力扣高频SQL 50题(基础版)第九题

时间:2025/7/9 21:04:24来源:https://blog.csdn.net/m0_70882914/article/details/140670214 浏览次数:0次

文章目录

  • 力扣高频SQL 50题(基础版)第九题
    • 197. 上升的温度
      • 题目说明
      • 思路分析
      • 实现过程
        • 准备数据
        • 实现方式
        • 结果截图
        • 总结

力扣高频SQL 50题(基础版)第九题

197. 上升的温度

题目说明

Weather

±--------------±--------+

| Column Name | Type |

±--------------±--------+

| id | int |

| recordDate | date |

| temperature | int |

±--------------±--------+

id 是该表具有唯一值的列。

没有具有相同 recordDate 的不同行。

该表包含特定日期的温度信息

编写解决方案,找出与之前(昨天的)日期相比温度更高的所有日期的 id

返回结果 无顺序要求

思路分析

在这里插入图片描述

找出与昨天相比温度更高的日期id的思路是使用自连接的方法。

实现过程

准备数据
Create table If Not Exists Weather (id int, recordDate date, temperature int)
Truncate table Weather
insert into Weather (id, recordDate, temperature) values ('1', '2015-01-01', '10')
insert into Weather (id, recordDate, temperature) values ('2', '2015-01-02', '25')
insert into Weather (id, recordDate, temperature) values ('3', '2015-01-03', '20')
insert into Weather (id, recordDate, temperature) values ('4', '2015-01-04', '30')
实现方式
select w2.id ID from Weather w1,Weather w2 where datediff(w2.recordDate,w1.recordDate)=1 and w1.temperature<w2.temperature;
结果截图

在这里插入图片描述

总结
时间日期函数datediff(end_date,start_date)函数:时间差
自连接查询语法:select * from 表A  t1, 表A t2 where  t1.xxid = t2.xxid;
关键字:力扣高频SQL 50题(基础版)第九题

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: