Rising Temperature

本文介绍了一种SQL查询方法,用于从气象数据表中找出所有日期的ID,这些日期的温度高于前一天的温度。提供了两种解决方案,一种使用子查询,另一种使用内连接,后者执行效率更高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.

+---------+------------+------------------+
| Id(INT) | Date(DATE) | Temperature(INT) |
+---------+------------+------------------+
|       1 | 2015-01-01 |               10 |
|       2 | 2015-01-02 |               25 |
|       3 | 2015-01-03 |               20 |
|       4 | 2015-01-04 |               30 |
+---------+------------+------------------+
For example, return the following Ids for the above Weather table:
+----+
| Id |
+----+
|  2 |
|  4 |
+----+
题意:找出记录中比前一天温度高的记录
用时1877ms
select id from Weather as w1 where  w1.temperature > (select temperature from Weather as w2 where w2.date = date_sub(w1.date, interval 1 day))
用时1290 ms 
select w1.id from Weather as w1 inner join Weather as w2 on to_days(w1.date) = to_days(w2.date) + 1 and w1.temperature > w2.temperature;
看来还是用连接查询要快些


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值