MySQL 中的日期时间等于或大于今天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5182275/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:58:26  来源:igfitidea点击:

Datetime equal or greater than today in MySQL

mysqlsqldatetime

提问by n00b

What's the best way to do following:

执行以下操作的最佳方法是什么:

SELECT * FROM users WHERE created >= today;

Note: created is a datetime field.

注意: created 是一个日期时间字段。

回答by Clodoaldo Neto

SELECT * FROM users WHERE created >= CURDATE();

But I think you mean created < today

但我认为你的意思是 created < today

回答by Shakti Singh

SELECT * FROM users WHERE created >= NOW();

if the column is datetime type.

如果列是日期时间类型。

回答by bart

If 'created' is datetime type

如果 'created' 是日期时间类型

SELECT * FROM users WHERE created < DATE_ADD(CURDATE(), INTERVAL 1 DAY);

CURDATE() means also '2013-05-09 00:00:00'

CURDATE() 也意味着“2013-05-09 00:00:00”

回答by JeffMan

Answer marked is misleading. The question stated is DateTime, but stated what was needed was just CURDATE().

标记的答案具有误导性。所陈述的问题是DateTime,但陈述所需要的只是CURDATE()

The shortest and correct answerto this is:

对此的最短和正确答案是:

SELECT * FROM users WHERE created >= CURRENT_TIMESTAMP;

回答by Bsienn

If the column have index and a function is applied on the column then index doesn't work and full table scan occurs, causing really slow query.

如果该列有索引并且在该列上应用了一个函数,则索引不起作用并且会发生全表扫描,从而导致查询速度非常慢。

To use index and compare datetimewith today/current date, the following can be used.

要使用索引并将日期时间与今天/当前日期进行比较,可以使用以下内容。

Solution for OP:

OP的解决方案:

select * from users
where created > CONCAT(CURDATE(), ' 23:59:59')

Sample to get data for today:

获取今天数据的示例:

select * from users
where 
    created >= CONCAT(CURDATE(), ' 00:00:00') AND
    created <= CONCAT(CURDATE(), ' 23:59:59')

Or use BETWEEN for short

或使用 BETWEEN 简称

select * from users 
where created BETWEEN 
      CONCAT(CURDATE(), ' 00:00:00') AND CONCAT(CURDATE(), ' 23:59:59')

回答by Silx

SELECT * FROM users WHERE created >= now()

回答by Rob Fahndrich

The below code worked for me.

下面的代码对我有用。

declare @Today date

Set @Today=getdate() --date will equal today    

Select *

FROM table_name
WHERE created <= @Today

回答by vpgodara

SELECT * FROM table_name WHERE CONCAT( SUBSTRING(json_date, 11, 4 ) ,  '-', SUBSTRING( json_date, 7, 2 ) ,  '-', SUBSTRING(json_date, 3, 2 ) ) >= NOW();

json_date ["05/11/2011"]

json_date ["05/11/2011"]

回答by Koney L Visinko

you can return all rows and than use php datediff function inside an if statement, although that will put extra load on the server.

您可以返回所有行,而不是在 if 语句中使用 php datediff 函数,尽管这会给服务器带来额外的负载。

if(dateDiff(date("Y/m/d"), $row['date']) <=0 ){    
}else{    
echo " info here";    
}