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 ;

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

相关推荐
想要打 Acm 的小周同学呀17 分钟前
Redis三剑客解决方案
数据库·redis·缓存
rkmhr_sef17 分钟前
Redis 下载与安装 教程 windows版
数据库·windows·redis
库库林_沙琪马2 小时前
Redis 缓存穿透、击穿、雪崩:问题与解决方案
数据库·redis·缓存
黄雪超2 小时前
大数据SQL调优专题——引擎优化
大数据·数据库·sql
LUCIAZZZ3 小时前
EasyExcel快速入门
java·数据库·后端·mysql·spring·spring cloud·easyexcel
落落落sss3 小时前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
wolf犭良3 小时前
19、《Springboot+MongoDB整合:玩转文档型数据库》
数据库·spring boot·mongodb
yuanbenshidiaos3 小时前
【正则表达式】
数据库·mysql·正则表达式
人类群星闪耀时3 小时前
深入理解 NoSQL 数据库:MongoDB 与 Cassandra
数据库·mongodb·nosql
yuanbenshidiaos3 小时前
【linux核心命令】
linux·服务器·数据库