197. Rising Temperature - 上升的温度 <easy>

表 Weather

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id 是这个表的主键
该表包含特定日期的温度信息
 

编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id 。返回结果 不要求顺序 。

查询结果格式如下例:

Weather
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1  | 2015-01-01 | 10          |
| 2  | 2015-01-02 | 25          |
| 3  | 2015-01-03 | 20          |
| 4  | 2015-01-04 | 30          |
+----+------------+-------------+

Result table:
+----+
| id |
+----+
| 2  |
| 4  |
+----+
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(30 -> 20)

Write an SQL query to find all dates' id with higher temperature compared to its previous dates (yesterday). Return the result table in any order.

分析:相邻两天日期的比较,Mysql内置函数中考虑使用:

1、TO_DAYS(date);

2、DATE_SUB(date,INTERVAL expr type) ; 

3、DATEDIFF(date1,date2);

# TO_DAYS(date) 返回从0年以来的天数
# DATEDIFF(b.recordDate,a.recordDate)=1 返回两个日期间隔的天数
# DATE_SUB(b.recordDate,INTERVAL 1 DAY) 返回两个日期间隔的天数

select b.id 
from Weather a,Weather b
where TO_DAYS(a.recordDate) = TO_DAYS(b.recordDate)-1 and b.Temperature > a.Temperature
#where a.recordDate = DATE_SUB(b.recordDate,INTERVAL 1 DAY) and b.Temperature > a.Temperature
#where DATEDIFF(b.recordDate,a.recordDate)=1 and b.Temperature > a.Temperature

 

 

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Car Vacation</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <header> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About Us</a></li> <li><a href="#">Destinations</a></li> <li><a href="#">Blog</a></li> <li><a href="#">Contact Us</a></li> </ul> </nav> <div class="hero-image"> <h1>Unleash Your Wanderlust</h1> <p>Let's Travel the World with Car Vacation</p> <button>Book Now</button> </div> </header> <section class="about-us"> <h2>About Us</h2> <p>Car Vacation is a leading provider of car rental and travel services around the globe. We offer a wide variety of rental cars from economy to luxury class and provide convenient travel packages for your dream vacation.</p> <p>Our team consists of experienced professionals who are passionate about delivering the best customer service and value. We believe in providing exceptional travel experience to our customers by offering quality services and personalized attention to detail.</p> </section> <section class="destinations"> <h2>Destinations</h2> <div class="destination"> <img src="images/destination1.jpg" alt="destination1"> <h3>New York</h3> <p>Discover the diverse culture and iconic landmarks of the Big Apple with our rental cars.</p> <button>Book Now</button> </div> <div class="destination"> <img src="images/destination2.jpg" alt="destination2"> <h3>Paris</h3> <p>Experience the romantic charm and artistic heritage of the City of Love on your own schedule.</p> <button>Book Now</button> </div> <div class="destination"> <img src="images/destination3.jpg" alt="destination3"> <h3>Tokyo</h3> <p>Explore the futuristic cityscape and rich cultural traditions of the Land of the Rising Sun.</p> <button>Book Now</button> </div> </section> <section class="blog"> <h2>From Our Blog</h2> <div class="blog-post"> <img src="images/blog1.jpg" alt="blog1"> <h3>5 Tips for Stress-Free Car Rentals</h3> <p>Here are some useful tips to help you navigate through the car rental process and avoid unnecessary stress during your trip.</p> <button>Read More</button> </div> <div class="blog-post"> <img src="images/blog2.jpg" alt="blog2"> <h3>Top 10 Best Road Trip Destinations in the World</h3> <p>If you're planning a road trip and want to explore some of the world's most stunning natural landscapes, check out our list of top 10 destinations.</p> <button>Read More</button> </div> <div class="blog-post"> <img src="images/blog3.jpg" alt="blog3"> <h3>How to Pack for a Car Rental Trip</h3> <p>Here are some practical tips on what to bring and what to leave behind when packing for your upcoming car rental trip.</p> <button>Read More</button> </div> </section> <section class="contact-us"> <h2>Contact Us</h2> <div class="contact-info"> <div> <i class="fa fa-map-marker"></i> <p>123 Main St., Suite 101<br>Anytown, USA 12345</p> </div> <div> <i class="fa fa-phone"></i> <p>1-800-CAR-VACA<br>1-800-227-8222</p> </div> <div> <i class="fa fa-envelope"></i> <p>vacations@car-vacation.com</p> </div> </div> <form> <label for="name">Name</label> <input type="text" id="name" name="name" required> <label for="email">Email</label> <input type="email" id="email" name="email" required> <label for="message">Message</label> <textarea id="message" name="message" required></textarea> <button>Send Message</button> </form> </section> <footer> <p>© 2022 Car Vacation. All rights reserved.</p> </footer> </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值