Select 两个日期之间的日期格式

Select between two dates dateformat

也许任何人都可以帮助我解决这个问题。我只想 select 两个日期之间的所有行,但日期格式有问题。

我的查询:

SELECT id, number, date
FROM `table`
WHERE (STR_TO_DATE(date, '%j-%n-%Y') between '6-4-2015' AND '6-4-2016')

我不知道这个查询有什么问题。我试过使用 STR_TO_DATEDATE

日期的日期类型是文本(我想保留它)

这是数据库的示例:

1     233        5-4-2015
2     238        6-4-2015
3     431        7-4-2015
4     230        8-4-2015

有人可以帮我吗?

plese try this one strtotime function

'.date("d-m-Y", strtotime($r['date'])).'

例如

 "select * from sales3 where (sdate between '.date("d-m-Y", strtotime($r['date1'])).' and '.date("d-m-Y", strtotime($r['date2'])).')"

您在 str_to_date 中进行了错误的日期转换,日期看起来是 d-m-Y 格式,而您所做的是

mysql> select STR_TO_DATE('5-4-2015', '%j-%n-%Y') ;
+-------------------------------------+
| STR_TO_DATE('5-4-2015', '%j-%n-%Y') |
+-------------------------------------+
| NULL                                |
+-------------------------------------+

所以它给你 null 并且商店到此为止。

你应该使用

mysql> select STR_TO_DATE('5-4-2015', '%d-%m-%Y') ;
+-------------------------------------+
| STR_TO_DATE('5-4-2015', '%d-%m-%Y') |
+-------------------------------------+
| 2015-04-05                          |
+-------------------------------------+
1 row in set (0.00 sec)

现在比较应该是

WHERE 
STR_TO_DATE(date, '%d-%m-%Y') 
between 
STR_TO_DATE('6-4-2015','%d-%m-%Y') AND STR_TO_DATE('6-4-2015','%d-%m-%Y')