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 ;

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

相关推荐
snow@li1 天前
数据库:市场中都有哪些数据库 / 优缺点 使用情况
数据库
NoSi EFUL1 天前
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
android·数据库·mysql
河阿里1 天前
SQL数据库:五大范式(NF)
数据库·sql·oracle
田梓燊1 天前
力扣:19.删除链表的倒数第 N 个结点
算法·leetcode·链表
l1t1 天前
DeepSeek总结的PostgreSQL 19查询提示功能
数据库·postgresql
chenxu98b1 天前
MySQL如何执行.sql 文件:详细教学指南
数据库·mysql
刘晨鑫11 天前
MongoDB数据库应用
数据库·mongodb
梦想的颜色1 天前
mongoTemplate + Java 增删改查基础介绍
数据结构·数据库·mysql
小小小米粒1 天前
redis命令集合
数据库·redis·缓存
Irene19911 天前
SQL 中日期的特殊性总结(格式符严格要求全大写)
sql