MySQL date_add() 在 WHERE 子句中不起作用?

发布于 2024-09-30 07:50:03 字数 231 浏览 3 评论 0原文

当我尝试运行以下查询时:

... WHERE `date` = DATE_ADD(NOW(), INTERVAL 10 HOUR)

它不起作用,因此我必须使用 $date = date("Ymd", strtotime('+10 hours'))

但为什么 date_add 不起作用?

非常感谢

When I try to run the following query:

... WHERE `date` = DATE_ADD(NOW(), INTERVAL 10 HOUR)

It doesn't work, so I have to use the $date = date("Y-m-d", strtotime('+10 hours'))

But why date_add doesn't work?

Thanks much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月下伊人醉 2024-10-07 07:50:03

请注意 DATE_ADD( ) 函数除了日期之外还返回时间部分。这与使用 php 的 date("Ymd", strtotime('+10 hours')) 不同,后者仅返回日期部分。

SELECT DATE_ADD(NOW(), INTERVAL 10 HOUR);
+-----------------------------------+
| DATE_ADD(NOW(), INTERVAL 10 HOUR) |
+-----------------------------------+
| 2010-11-05 01:59:51               |
+-----------------------------------+
1 row in set (0.00 sec)

您可以使用 WHERE date = DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR)) 代替:

SELECT DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR));
+-----------------------------------------+
| DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR)) |
+-----------------------------------------+
| 2010-11-05                              |
+-----------------------------------------+
1 row in set (0.02 sec)

测试用例:

CREATE TABLE tbl (id int, date datetime);

INSERT INTO tbl VALUES (1, '2010-11-04');
INSERT INTO tbl VALUES (2, '2010-11-05');
INSERT INTO tbl VALUES (3, '2010-11-06');
INSERT INTO tbl VALUES (4, '2010-11-07');

SELECT * FROM tbl WHERE date = DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR));
+------+---------------------+
| id   | date                |
+------+---------------------+
|    2 | 2010-11-05 00:00:00 |
+------+---------------------+
1 row in set (0.00 sec)

Note that DATE_ADD() function returns a time part in addition to the date. This is not the same as using php's date("Y-m-d", strtotime('+10 hours')), which only returns the date part.

SELECT DATE_ADD(NOW(), INTERVAL 10 HOUR);
+-----------------------------------+
| DATE_ADD(NOW(), INTERVAL 10 HOUR) |
+-----------------------------------+
| 2010-11-05 01:59:51               |
+-----------------------------------+
1 row in set (0.00 sec)

You could use WHERE date = DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR)) instead:

SELECT DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR));
+-----------------------------------------+
| DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR)) |
+-----------------------------------------+
| 2010-11-05                              |
+-----------------------------------------+
1 row in set (0.02 sec)

Test case:

CREATE TABLE tbl (id int, date datetime);

INSERT INTO tbl VALUES (1, '2010-11-04');
INSERT INTO tbl VALUES (2, '2010-11-05');
INSERT INTO tbl VALUES (3, '2010-11-06');
INSERT INTO tbl VALUES (4, '2010-11-07');

SELECT * FROM tbl WHERE date = DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR));
+------+---------------------+
| id   | date                |
+------+---------------------+
|    2 | 2010-11-05 00:00:00 |
+------+---------------------+
1 row in set (0.00 sec)
盗梦空间 2024-10-07 07:50:03

NOW() 是时间戳,而不是日期。

示例:

mysql> select now(), current_date();
+---------------------+----------------+
| now()               | current_Date() |
+---------------------+----------------+
| 2010-11-04 12:00:01 | 2010-11-04     |
+---------------------+----------------+

我认为这就是您想要的:

... WHERE date = DATE_ADD(CURRENT_DATE(), INTERVAL 10 HOUR)

NOW() is a timestamp, not a date.

Example:

mysql> select now(), current_date();
+---------------------+----------------+
| now()               | current_Date() |
+---------------------+----------------+
| 2010-11-04 12:00:01 | 2010-11-04     |
+---------------------+----------------+

I think this is what you want:

... WHERE date = DATE_ADD(CURRENT_DATE(), INTERVAL 10 HOUR)
秋日私语 2024-10-07 07:50:03

您将 DateTime(DATE_ADD 的结果)与 DATE 进行比较。您需要WHERE date = DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR))

You compare DateTime (result of DATE_ADD) with DATE. You need WHERE date = DATE(DATE_ADD(NOW(), INTERVAL 10 HOUR))

心如荒岛 2024-10-07 07:50:03

这:

WHERE date = DATE_ADD(NOW(), INTERVAL 10 HOUR)

...确实有效(假设date是DATETIME),但它包括执行语句时的时间部分——date`值需要与< em>完全要返回。

This:

WHERE date = DATE_ADD(NOW(), INTERVAL 10 HOUR)

...does work (assuming date is DATETIME), but it includes the time portion when the statement was executed -- thedate` values need to match that exactly to be returned.

热鲨 2024-10-07 07:50:03

我知道这个问题已经得到解答,但在这个用例中有一个更好的解决方案:

DATE_ADD(TODAY(), INTERVAL 10 HOUR)

这会删除一个额外的操作,因为 TODAY 返回日期而没有时间部分。

I know this is answered already but there is a better solution in this usecase:

DATE_ADD(TODAY(), INTERVAL 10 HOUR)

This removes one additional operation because TODAY returns date without time part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文