MySQL:如何在查询中的日期时间字段中添加一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2810516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: How to add one day to datetime field in query
提问by Daniel Vassallo
In my table I have a field named eventdate
in datetime
format like 2010-05-11 00:00:00
.
在我的表我有一个名为领域eventdate
中datetime
类似的格式2010-05-11 00:00:00
。
How do i make a query so that it adds one dayto the eventdate
eg if today is 2010-05-11
, i want to show in where
clause to return all records with tomorrow's date.
我如何进行查询,以便将一天添加到eventdate
例如,如果今天是2010-05-11
,我想在where
子句中显示以返回具有明天日期的所有记录。
Update:
更新:
I tried this:
我试过这个:
select * from fab_scheduler where custid = 1334666058 and DATE_ADD(eventdate, INTERVAL 1 DAY)
select * from fab_scheduler where custid = 1334666058 and DATE_ADD(eventdate, INTERVAL 1 DAY)
But unfortunately it returns the same record even if i add an interval greater than 1.
但不幸的是,即使我添加了大于 1 的间隔,它也会返回相同的记录。
Result:
结果:
2010-05-12 00:00:00
2010-05-12 00:00:00
But i only want to select records with tomorrow's date.
但我只想选择明天日期的记录。
回答by Daniel Vassallo
You can use the DATE_ADD()
function:
您可以使用该DATE_ADD()
功能:
... WHERE DATE(DATE_ADD(eventdate, INTERVAL -1 DAY)) = CURRENT_DATE
It can also be used in the SELECT
statement:
也可以用在SELECT
语句中:
SELECT DATE_ADD('2010-05-11', INTERVAL 1 DAY) AS Tomorrow;
+------------+
| Tomorrow |
+------------+
| 2010-05-12 |
+------------+
1 row in set (0.00 sec)
回答by David Yell
Have a go with this, as this is how I would do it :)
试试这个,因为这就是我要做的:)
SELECT *
FROM fab_scheduler
WHERE custid = '123456'
AND CURDATE() = DATE(DATE_ADD(eventdate, INTERVAL 1 DAY))
回答by Vadim
It`s possible to use MySQL specific syntax sugar:
可以使用 MySQL 特定的语法糖:
SELECT ... date_field + INTERVAL 1 DAY
Looks much more pretty instead of DATE_ADD function
看起来更漂亮,而不是 DATE_ADD 函数
回答by vzr
If you are able to use NOW() this would be simplest form:
如果您能够使用 NOW() 这将是最简单的形式:
SELECT * FROM `fab_scheduler` WHERE eventdate>=(NOW() - INTERVAL 1 DAY)) AND eventdate<NOW() ORDER BY eventdate DESC;
With MySQL 5.6+ query abowe should do. Depending on sql server, You may be required to use CURRDATE()
instead of NOW()
- which is alias for DATE(NOW())
and will return only date part of datetime
data type;
使用 MySQL 5.6+ 查询应该可以。根据 sql server,您可能需要使用CURRDATE()
而不是NOW()
- 它是别名,DATE(NOW())
并且将仅返回datetime
数据类型的日期部分;
回答by Amit Prajapati
You can try this:
你可以试试这个:
SELECT DATE(DATE_ADD(m_inv_reqdate, INTERVAL + 1 DAY)) FROM tr08_investment
回答by ErikS
How about this:
这个怎么样:
select * from fab_scheduler where custid = 1334666058 and eventdate = eventdate + INTERVAL 1 DAY
回答by Vonder
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
Or, simplier:
或者,更简单:
date("Y-m-d H:i:s", time()+((60*60)*24));