我在 Leetcode 上遇到过这个 MySQL 问题。给定表温度
+---------+------------+------------------+
| Id(INT) | Date(DATE) | Temperature(INT) |
+---------+------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------+------------------+
目标是编写一个 SQL 查询来查找与之前(昨天)日期相比温度更高的所有日期 ID。
所以我写了一些如下内容,通过了 13/14 测试用例:
select W1.Id
from Weather as W1, Weather as W2
where W1.Date-1 = W2.Date and W1.Temperature> W2.Temperature
我在论坛上查了一个解决方案,发现了一个类似的解决方案,并且通过了所有案例:
select W1.Id
from Weather as W1, Weather as W2
where subdate(W1.Date,1) = W2.Date and W1.Temperature> W2.Temperature
所以我的问题是 W1.Date-1 和 subdate(W1.Date,1) 之间有什么区别,因为 subdate 也从 W1.Date 中减去 1 天?
最佳答案
在mysql中也是一样的。 你可以在你的mysql数据库中进行测试。
关于mysql - MySQL中SUB_DATE()函数和直接从日期中减去数字之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307769/