196.刪除重復的電子郵箱
Person
結果
方法一:
delete p1
from Person p1,Person p2
where p1.Email = p2.Email and p1.Id <p2.Id
197.上升的溫度
給定一個 Weather 表,編寫一個 SQL 查詢卢未,來查找與之前(昨天的)日期相比溫度更高的所有日期的 Id匆骗。
Weather
結果
方法一:
select w1.Id
from Weather w1, Weather w2
where w1. Temperature>w2. Temperature and subdata(w1.RecordDate,1)=w2.RecordDate
分析:該方法使用MySQL的SUBDATE函數(shù)卿捎,實現(xiàn)日期減一。
方法二:
select w1.Id
from Weather w1, Weather w2
where w1. Temperature>w2. Temperature and datediff(w1.RecordDate,w2.RecordDate)=1
分析:該方法使用MySQL的DataDiff函數(shù)計算兩個日期的差值周蹭。
方法三:
select w1.Id
from Weather w1, Weather w2
where w1. Temperature>w2. Temperature and to_days(w1.RecordDate) - to_days(w2.RecordDate) = 1
分析:該方法使用MySQL的TO_DAYS函數(shù)趋艘,用來將日期換算成天數(shù),再進行減法比較凶朗。