★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-njjwcksc-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
SQL架構git
1 Create table If Not Exists Weather (Id int, RecordDate date, Temperature int) 2 Truncate table Weather 3 insert into Weather (Id, RecordDate, Temperature) values ('1', '2015-01-01', '10') 4 insert into Weather (Id, RecordDate, Temperature) values ('2', '2015-01-02', '25') 5 insert into Weather (Id, RecordDate, Temperature) values ('3', '2015-01-03', '20') 6 insert into Weather (Id, RecordDate, Temperature) values ('4', '2015-01-04', '30')
Given a Weather
table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.github
+---------+------------------+------------------+ | Id(INT) | RecordDate(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 | +----+
給定一個 Weather
表,編寫一個 SQL 查詢,來查找與以前(昨天的)日期相比溫度更高的全部日期的 Id。架構
+---------+------------------+------------------+ | Id(INT) | RecordDate(DATE) | Temperature(INT) | +---------+------------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------------+------------------+
例如,根據上述給定的 Weather
表格,返回以下 Id:spa
+----+ | Id | +----+ | 2 | | 4 | +----+
163ms
1 # Write your MySQL query statement below 2 SELECT t.id 3 FROM (SELECT i.*, 4 IF(@last_date + interval 1 day = RecordDate and @last_temp < Temperature, 1, 0) AS rownum, 5 @last_temp := Temperature, 6 @last_date := RecordDate 7 FROM (select @last_date := null, @last_temp := null) AS _init 8 , Weather i 9 ORDER BY i.RecordDate) AS t 10 WHERE t.rownum = 1;
166mscode
1 # Write your MySQL query statement below 2 select b.keepId as Id 3 from (select if(Temperature > @prevTemp and datediff(RecordDate, @prevDate) = 1, a.Id, NULL) as keepId, 4 @prevTemp := Temperature, @prevDate := RecordDate 5 from (select * from Weather order by RecordDate) a, 6 (select @prevTemp := NULL, @prevDate := NULL) init) b 7 where keepId is not NULL
169mshtm
1 # Write your MySQL query statement below 2 3 SELECT Id 4 FROM 5 ( SELECT Id, Temperature, 6 @Higher := If(@Pretemp < Temperature AND 1 = DATEDIFF(RecordDate,@Predate), 1, 0) h, 7 @Predate := RecordDate predate, 8 @Pretemp := Temperature pretemp 9 FROM (SELECT @Higher := 0, @Pretemp := 101, @Predate := 1000-01-01) w1, 10 (SELECT * FROM Weather ORDER BY RecordDate) w2 11 ) t WHERE t.h = 1
170msblog
1 # Write your MySQL query statement below 2 select f2.Id from (select f.Id, if(Temperature>@prev and datediff(RecordDate, @prevDate)=1, 1, 0) as rising, @prev:=Temperature, @prevDate:=RecordDate from (select Id, RecordDate, Temperature from (select * from Weather order by RecordDate asc) ordered, (select @rising:=NULL, @prev:=NULL, @prevDate:=NULL) tmp) f) f2 where f2.rising = 1 order by f2.Id;