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 ;

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

相关推荐
七度黑光3 小时前
用 openclaw 给故障复盘打分:质量审核自动化实践
运维·服务器·前端·数据库·自动化
阿豪学编程4 小时前
LeetCode724.:寻找数组的中心下标
算法·leetcode
华科易迅5 小时前
Spring 事务(注解)
java·数据库·spring
Java面试题总结5 小时前
MySQL篇 索引失效
数据库·mysql
last demo5 小时前
mysql
运维·数据库·mysql·oracle
禹中一只鱼5 小时前
【力扣热题100学习笔记】 - 哈希
java·学习·leetcode·哈希算法
凌波粒5 小时前
LeetCode--349.两个数组的交集(哈希表)
java·算法·leetcode·散列表
kevin_cat6 小时前
oracle 扩展表空间
数据库·oracle
花间相见7 小时前
【MySQL面试题】—— MySQL面试高频问题汇总:从原理到实战,覆盖90%考点
数据库·mysql·面试
高梦轩8 小时前
MySQL 数据库备份与恢复
数据库·oracle