SQL 上升的温度

197 上升的温度

SQL架构

表: 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)

解决方案:

提供思路

如同之前的思路,该表join自身一次(cross join 笛卡尔集,详细可以百度查询),筛选条件。由于不同的sql有类似的语法,下面写出相关语法。

上代码:

sql 复制代码
//1
select a.id
from weather as a cross join weather as b 
     on datediff(a.recordDate, b.recordDate) = 1
where a.Temperature  > b.Temperature ;

//2
select a.id
from weather as a cross join weather as b 
     on timestampdiff(day, a.recordDate , b.recordDate ) = -1
where a.Temperature  > b.Temperature ;

以上是碰到的第一百九十七题,后续持续更新。感觉对你有帮助的小伙伴可以帮忙点个赞噢!

相关推荐
一起养小猫6 分钟前
LeetCode100天Day8-缺失数字与只出现一次的数字
java·数据结构·算法·leetcode
梭七y9 分钟前
【力扣hot100题】(115)缺失的第一个正数
数据结构·算法·leetcode
Dxy123931021643 分钟前
Python MySQL 错误回滚实战代码
数据库·python·mysql
Go高并发架构_王工1 小时前
Redis未来展望:Redis 7.0新特性与技术发展趋势
数据库·redis·缓存
产幻少年1 小时前
用户登录日志表和系统日志
运维·服务器·数据库
·云扬·2 小时前
InnoDB Cluster高可用测试实战:主从切换与故障恢复验证
数据库·mysql
qq_455760852 小时前
redis - 持久化
数据库·redis·缓存
&友情岁月&2 小时前
sql脚本的union的要注意点
数据库·sql
nvd112 小时前
基于 LangChain + Gemini + CloudSQL (pgvector) 的 RAG 实现指南
数据库·langchain
一起养小猫3 小时前
LeetCode100天Day9-无重复字符的最长子串与赎金信
java·开发语言·数据结构·leetcode