如果年份不同,则无法获得“中间”记录

时间:2016-03-01 06:00:50

标签: php mysql

我有这个独特的错误

TABLE : tbl
id | title | iscancel | sold_dt

id: UID
Title : varchar
iscancel : 0/1
sold_dt : timestamp


select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "06/01/2015" AND "03/01/2016" GROUP BY day(sold_dt) order by (sold_dt) asc 

(注意年份不同)

  • 返回0条记录

但如果我这样做

select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "06/01/2015" AND "12/01/2015" GROUP BY day(sold_dt) order by (sold_dt) asc 

select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "01/01/2016" AND "03/01/2016" GROUP BY day(sold_dt) order by (sold_dt) asc 

(注意年份相同)

- 我会得到一些记录

*使用DATE_FORMAT因为我的日历正在给我MM/DD/YYYY(我无法更改,因为它可能会影响其他区域)

我做错了什么?

2 个答案:

答案 0 :(得分:2)

不要将存储的值转换为其他格式,而是尝试将参数本身更改为正确的格式。如果您无法在您的网站上进行此操作,您也可以在查询中执行此操作

sold_dt BETWEEN
STR_TO_DATE('06/01/2015', '%m/%d/%Y') AND STR_TO_DATE('03/01/2016', '%m/%d/%Y')    

答案 1 :(得分:0)

尝试将静态日期传递到DATE_FORMAT()函数

select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN DATE_FORMAT("06/01/2015","%m/%d/%Y") AND DATE_FORMAT("03/01/2016","%m/%d/%Y") GROUP BY day(sold_dt) order by (sold_dt) asc