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 ;

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

相关推荐
u***28472 分钟前
SpringBoot集成Flink-CDC,实现对数据库数据的监听
数据库·spring boot·flink
m***11902 分钟前
Ubuntu 系统下安装 Nginx
数据库·nginx·ubuntu
e***98574 分钟前
Window下Redis的安装和部署详细图文教程(Redis的安装和可视化工具的使用)
数据库·redis·缓存
xingyue_S4 分钟前
MySQL 数据库(一) -- 初体验
数据库·mysql·oracle
X***C86211 分钟前
【Oracle11g SQL详解】UPDATE 和 DELETE 操作的正确使用
数据库·sql
爬山算法11 分钟前
Redis(156)Redis的延迟问题如何解决?
数据库·redis·缓存
v***913011 分钟前
Windows版Redis本地后台启动
数据库·windows·redis
x***440112 分钟前
【prometheus】监控MySQL并实现可视化
数据库·mysql·prometheus
h***047712 分钟前
MySQL 的 INSERT(插入数据)详解
android·数据库·mysql
梁bk13 分钟前
Redis 数据类型(上)String,List,Set
数据库·redis