简体   繁体   English

如何在MySQL中动态设置从00:00开始的昨天日期?

[英]How to set dynamically the yesterday date starting from 00:00 in mySQL?

I want to dynamically set in mysql this: 我想在mysql中动态设置:

2017-07-26 00:00:00

for yesterday's time, like I do with now() for now. 在昨天的时间里,就像我现在使用now()

How to do this? 这个怎么做?

You can use MySQL's DATE function to extract date part from datetime, eg: 您可以使用MySQL的DATE函数从datetime中提取日期部分,例如:

SELECT DATE(NOW());

This will give you current date without time. 这将为您提供当前日期,而无需时间。

If you want yesterday's date, you can use DATE_ADD to subtract 1 day and use DATE function on the result to get the date without time, eg: 如果需要昨天的日期,则可以使用DATE_ADD减去1天,然后对结果使用DATE函数以获取没有时间的日期,例如:

SELECT DATE(DATE_ADD(NOW(), INTERVAL -1 DAY));

Here's the documentation for DATE and DATE_ADD functions. 这是DATEDATE_ADD函数的文档。

Update 更新

If you want seconds as 00:00:00 then you can use CONCAT method, eg: 如果您希望秒为00:00:00则可以使用CONCAT方法,例如:

SELECT CONCAT(DATE(DATE_ADD(NOW(), INTERVAL -1 DAY)), ' 00:00:00');

您可以使用TIMESTAMP()函数将日期转换为带有00:00:00作为时间部分的时间戳:

select timestamp(curdate() - interval 1 day);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM