比较 MySQL 中的日期

我想从数据库中比较两个给定日期之间的日期。 数据库中的列是 DATETIME,我只想将其与日期格式进行比较,而不是与日期时间格式进行比较。

SELECT * FROM `players` WHERE CONVERT(CHAR(10),us_reg_date,120) >= '2000-07-05' AND CONVERT(CHAR(10),us_reg_date,120) <= '2011-11-10'

I get this error when I execute the SQL above:

您的 SQL 语法有错误; 检查对应的手册 的 MySQL 服务器版本 使用的正确语法 'us_reg_date,120) >= '2000-07-05' AND CONVERT (CHAR (10) ,us _ reg _ date,120) < = '2011-' at line 1

这个问题怎么解决?

410607 次浏览

That is SQL Server 语法 for converting a date to a string. In MySQL you can use the 日期 function to extract the date from a datetime:

SELECT *
FROM players
WHERE DATE(us_reg_date) BETWEEN '2000-07-05' AND '2011-11-10'

但是如果你想利用 us_reg_date列上的索引,你可以试试这个:

SELECT *
FROM players
WHERE us_reg_date >= '2000-07-05'
AND us_reg_date < '2011-11-10' + interval 1 day

您可以尝试下面的查询,

select * from players
where
us_reg_date between '2000-07-05'
and
DATE_ADD('2011-11-10',INTERVAL 1 DAY)

这种方法是有效的:

select date_format(date(starttime),'%Y-%m-%d') from data
where date(starttime) >= date '2012-11-02';

请注意格式字符串 %Y-%m-%d和输入日期的格式。

我知道答案了。

密码如下:

SELECT * FROM table
WHERE STR_TO_DATE(column, '%d/%m/%Y')
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')

this is what it worked for me:

select * from table
where column
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')

Please, note that I had to change STR_TO_DATE(column, '%d/%m/%Y') from previous solutions, as it was taking ages to load