一、题目
输出比前一天温度高的序列号。
二、解题
1.SUBDATE()函数
SELECT wt1.Id
FROM Weather wt1,Weather wt2
WHERE SUBDATE(wt1.RecordDate,interval 1 day)=wt2.RecordDate
&& wt1.Temperature>wt2.Temperature;
解析:
因为需要与前一天的温度进行对比,所以需要命名两张表格。
SUBDATE(date,INTERVAL expr type )date 参数是合法的日期表达式。expr 参数是您希望添加的时间间隔。type 参数可以是:
2.DATEDIFF()函数
SELECT wt1.Id
FROM Weather wt1,Weather wt2
WHERE DATEDIFF(wt1.RecordDate,wt2.RecordDate)=1
&& wt1.Temperature>wt2.Temperature;
解析:DATEDIFF函数返回两个日期之间的天数。
3.TO_DAYS()函数
SELECT wt1.Id
FROM Weather wt1,Weather wt2
WHERE TO_DAYS(wt1.RecordDate)-TO_DAYS(wt2.RecordDate)=1
&& wt1.Temperature>wt2.Temperature;
解析:TO_DAYS()函数返回从年份0开始的天数。
4.INNER JOIN
select a.Id
from Weather a inner join Weather b
on TO_DAYS(a.RecordDate)-TO_DAYS(b.RecordDate) = 1
where a.Temperature>b.Temperature;
解析:使用内级联的方式,on后面是级联的条件。
5.级联条件不使用函数
select a.Id
from Weather a inner join Weather b
on a.RecordDate - interval 1 day = b.RecordDate
where a.Temperature>b.Temperature;