php MySQL如何从今天记录的表中选择数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7683290/
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 SELECT data from table which recorded today?
提问by AJ OP
Use PHP and MySQL. In my table, there is date field (datetime) recorded by NOW() sql function. Example value of data in this field is 2010-10-07 10:57:36. How can I SELECT all data which day-month-year is today. I try to use code as below:
使用 PHP 和 MySQL。在我的表中,有 NOW() sql 函数记录的日期字段(datetime)。此字段中数据的示例值为 2010-10-07 10:57:36。如何选择今天是日-月-年的所有数据。我尝试使用如下代码:
SELECT * FROM table WHERE date=????
回答by Kaivosukeltaja
Try this:
尝试这个:
SELECT * FROM table WHERE date > CURDATE();
CURDATE()
will return the current date as 2011-10-07
which will be cast to 2011-10-07 00:00:00
when comparing datetime
s to it.
CURDATE()
将返回当前日期2011-10-07
将被转换为2011-10-07 00:00:00
比较时datetime
s到它。
Note that if you use DATE(date) = CURDATE()
you will run a date conversion for everyrow in the table, which will be really bad for your perfomance if you have many rows and/or you need to run the query often. Also make sure you have an index on date
, otherwise both methods will be even slower.
请注意,如果您使用,DATE(date) = CURDATE()
您将为表中的每一行运行日期转换,如果您有很多行和/或您需要经常运行查询,这将对您的性能非常不利。还要确保你有一个索引date
,否则这两种方法都会更慢。
回答by Sergey Kudriavtsev
SELECT * FROM table where DATE(date)=CURDATE()
SELECT * FROM table where DATE(date)=CURDATE()
回答by Bryan
SELECT * FROM tableName WHERE DATE(fieldDate) = DATE(NOW());
回答by michael667
The date_format
function allows you to easily switch between various granularities:
该date_format
功能可让您轻松地在各种粒度之间切换:
Select everything from the same day:
选择同一天的所有内容:
select * from table
where date_format(date, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d');
From the same month:
同月起:
select * from table
where date_format(date, '%Y-%m') = date_format(now(), '%Y-%m');
From the same year:
同年:
select * from table
where date_format(date, '%Y') = date_format(now(), '%Y');
From the same hour:
从同一小时开始:
select * from table
where date_format(date, '%Y-%m-%d %H') = date_format(now(), '%Y-%m-%d %H');
and so on.
等等。
回答by vivek
Try this
尝试这个
SELECT * FROM table WHERE DATE(my_date)=DATE(now())
my_date -> column name
回答by Fredy
SET @day = '2017-12-12' ;
SELECT * FROM table WHERE dateColumn BETWEEN DATE(@day) AND DATE_ADD(DATE(@day), INTERVAL 1 DAY ) ;
回答by Selman Akdemir
use something like this it exactly works on my code(access database):
使用这样的东西它完全适用于我的代码(访问数据库):
select * from Table t where t.column>=Date() and t.column< Date() + 1
回答by denolk
use between.
之间使用。
select * from table date between '2010-10-06' and '2010-10-08';