[英]Creating correct date format for SELECT…BETWEEN query
I have 2 variables which make up a date range which then needs to be searched in a MySQL database. 我有2个变量组成一个日期范围,然后需要在MySQL数据库中进行搜索。
These are in the format 'Apr, 2011' and 'Mar, 2012' etc. 这些格式为“ 2011年4月”和“ 2012年3月”等。
My SELECT...BETWEEN query requires '2011-04-01' and '2012-03-31', so whats the best way to provide this... 我的SELECT ... BETWEEN查询需要'2011-04-01'和'2012-03-31',那么提供此功能的最佳方法是什么...
Convert the dates in PHP first? 首先转换PHP中的日期?
Or is there a way to write the query to search using the original format? 还是有一种方法可以编写查询以使用原始格式进行搜索? I tried with date_format (date, '%b, %Y') but the query found nothing.
我尝试使用date_format(日期,'%b,%Y'),但查询未找到任何内容。
Try this: 尝试这个:
SELECT * FROM tablename
WHERE dateCol BETWEEN DATE_ADD(DATE(STR_TO_DATE('Apr, 2011', '%b, %Y')), INTERVAL 1 DAY) AND LAST_DAY(STR_TO_DATE('Mar, 2012', '%b, %Y'));
Check the SQL FIDDLE DEMO 检查SQL FIDDLE DEMO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.