【发布时间】:2011-12-02 18:06:27
【问题描述】:
使用 PHP 和 MySQL。在我的表中,有 NOW() sql 函数记录的日期字段(日期时间)。该字段中数据的示例值为2010-10-07 10:57:36。如何选择今天的日月年的所有数据。我尝试使用如下代码:
SELECT * FROM table WHERE date=????
【问题讨论】:
使用 PHP 和 MySQL。在我的表中,有 NOW() sql 函数记录的日期字段(日期时间)。该字段中数据的示例值为2010-10-07 10:57:36。如何选择今天的日月年的所有数据。我尝试使用如下代码:
SELECT * FROM table WHERE date=????
【问题讨论】:
SET @day = '2017-12-12' ;
SELECT * FROM table WHERE dateColumn BETWEEN DATE(@day) AND DATE_ADD(DATE(@day), INTERVAL 1 DAY ) ;
【讨论】:
使用这样的东西,它完全适用于我的代码(访问数据库):
select * from Table t where t.column>=Date() and t.column< Date() + 1
【讨论】:
试试这个
SELECT * FROM table WHERE DATE(my_date)=DATE(now())
my_date -> column name
【讨论】:
试试这个:
SELECT * FROM table WHERE date > CURDATE();
CURDATE() 将返回当前日期为2011-10-07,在将datetimes 与它进行比较时,它将被转换为2011-10-07 00:00:00。
请注意,如果您使用DATE(date) = CURDATE(),您将为表格中的每一行运行日期转换,如果您有很多行和/或您需要经常运行查询。还要确保你在date 上有一个索引,否则这两种方法都会更慢。
【讨论】:
GETDATE()。
date_format功能让你轻松切换各种粒度:
选择同一天的所有内容:
select * from table
where date_format(date, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d');
从同一个月开始:
select * from table
where date_format(date, '%Y-%m') = date_format(now(), '%Y-%m');
同年:
select * from table
where date_format(date, '%Y') = date_format(now(), '%Y');
从同一时间开始:
select * from table
where date_format(date, '%Y-%m-%d %H') = date_format(now(), '%Y-%m-%d %H');
等等。
【讨论】:
SELECT * FROM tableName WHERE DATE(fieldDate) = DATE(NOW());
【讨论】:
SELECT * FROM table where DATE(date)=CURDATE()
【讨论】:
在之间使用。
select * from table date between '2010-10-06' and '2010-10-08';
【讨论】: