[英]how to search records beetween two dates. But table date format is varchar
I have a table. 我有桌子 In which, a column name is tb_date
, which is varchar
format. 其中,列名是tb_date
,这是varchar
格式。 In that column dates are save, But in different-different format (Like: 01/07/201 OR 01-08-2018
or 2017/03/12
etc.). 在该列中保存日期,但格式不同(例如: 01/07/201 OR 01-08-2018
或2017/03/12
等)。 Now I want a search between given date. 现在我要在给定日期之间进行搜索。 But it is not working. 但这是行不通的。 I tried it- 我尝试过这个-
SELECT * FROM `user_History` WHERE date_format(str_to_date(`tb_date`, '%d/%m/%Y'), '%d/%m/%Y') BETWEEN '01/06/2018' AND '31/06/2018'
But its giving all record. 但它给出了所有记录。 I tried it in my sql. 我在SQL中尝试过。
Whats is the problem? 有什么问题吗?
Problem is in your str_to_date(tb_date, '%d/%m/%Y')
, you give a format %d/%m/%Y
of importing date, but you have different formats in this field. 问题出在您的str_to_date(tb_date, '%d/%m/%Y')
,您提供了导入日期的格式为%d/%m/%Y
,但是此字段中的格式不同。
I think, you should process all your table by PHP and, for example, convert all your dates to UNIX_TIMESTAMP by function strtotime()
. 我认为,您应该通过PHP处理所有表,例如,通过功能strtotime()
将所有日期转换为UNIX_TIMESTAMP。
It will be more easy than try to create a SQL query for it. 这比尝试为其创建SQL查询要容易得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.