简体   繁体   English

使用日期格式的MYSQL DATE范围查询

[英]MYSQL DATE range Query with date format

I have a MYSQL table with field name "date" in the formate "dd/mm"; 我有一个格式为“ dd / mm”的字段名称为“ date”的MYSQL表;

I need to query results from 20/06 to 22/06 (3 days data), this is how I am trying to do it. 我需要查询从20/06到22/06(3天数据)的结果,这就是我要尝试的方式。

mysql> SELECT * FROM `wtt` WHERE date BETWEEN '20/06' AND '22/06';

But MYSQL is also selecting the dates 22/04, 22/05 and other entries. 但是,MYSQL还选择日期22 / 04、22 / 05和其他条目。

Is there MYSQL query which will fetch me the right answer without the need to change the format in which I have already stored the information in the MYSQL table? 是否有MYSQL查询可以获取正确答案,而无需更改已将信息存储在MYSQL表中的格式?

Thank you all in advance! 谢谢大家!

You should convert the dates to MySQL date types and then compare them. 您应该将日期转换为MySQL日期类型,然后进行比较。

You can convert the dates like this: 您可以像这样转换日期:

SELECT STR_TO_DATE(CONCAT('20/06', '/', EXTRACT(YEAR FROM CURDATE())),'%d/%m/%Y');

or simply 或者干脆

SELECT STR_TO_DATE('20/06','%d/%m');

for year 0000 if it doesn't matter. 如果没有关系,则为0000年。

Storing dates as strings has brought this problem on you. 将日期存储为字符串会给您带来这个问题。 You should always use DATE type for storing dates. 您应该始终使用DATE类型存储日期。

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

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