LeetCode--197.上升的温度

在这里插入图片描述

建表

-- ----------------------------
-- Table structure for `weather`
-- ----------------------------
DROP TABLE IF EXISTS `weather`;
CREATE TABLE `weather` (
 `Id` int(11) DEFAULT NULL,
 `RecordDate` date DEFAULT NULL,
 `Temperature` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of weather
-- ----------------------------
INSERT INTO `weather` VALUES ('1','2015-01-01', '10');
INSERT INTO `weather` VALUES ('2','2015-01-02', '25');
INSERT INTO `weather` VALUES ('3','2015-01-03', '20');
INSERT INTO `weather` VALUES ('4','2015-01-04', '30');

第一种思路,Datadiff来计算两个日期的差值,然后条件是温度高且差一天

-- w1当做今天,w2当做昨天,今天温度比昨天高,且相差一天
select w1.* from weather w1, weather w2
 where w1.Temperature > w2.Temperature
 and DATEDIFF(w1.RecordDate,w2.RecordDate) = 1

第二种思路,TO_DAYS将日期转换为天,条件同上

select w1.* from weather w1, weather w2
 where w1.Temperature > w2.Temperature
 and TO_DAYS(w1.RecordDate) = TO_DAYS(w2.RecordDate) + 1

第三种思路,可以用Subdate函数来实现日期减1

select w1.* from weather w1, weather w2
 where w1.Temperature > w2.Temperature
 and SUBDATE(w1.RecordDate, 1) = w2.RecordDate

猜你喜欢

转载自blog.csdn.net/qq_42363032/article/details/108908448