Mysql 将日期条件添加到where子句时未给出结果的查询

Mysql 将日期条件添加到where子句时未给出结果的查询,mysql,sql,hibernate,Mysql,Sql,Hibernate,如果未添加两个日期之间的where子句,下面的查询将返回正确的结果,否则将返回零,这是我创建的hibernate查询的sql等价物。 在我的数据库中,日期保存为ex:-2014年2月12日 谁能给我一个解决方案,如何获取两个给定日期之间的结果 select distinct count(customerjob0_.JOB_NUMBER) as col_0_0_ from JOB_CUSTOMER customerjob0_ left outer join SERVICE_LOCATION ser

如果未添加两个日期之间的where子句,下面的查询将返回正确的结果,否则将返回零,这是我创建的hibernate查询的sql等价物。 在我的数据库中,日期保存为ex:-2014年2月12日 谁能给我一个解决方案,如何获取两个给定日期之间的结果

select distinct count(customerjob0_.JOB_NUMBER) as col_0_0_ from JOB_CUSTOMER customerjob0_
left outer join SERVICE_LOCATION serviceloc1_ on customerjob0_.SERVICE_LOCATION_CODE=serviceloc1_.SERVICE_LOCATION_CODE 
left outer join JOB_CONTRACTS jobcontrac2_ on customerjob0_.JOB_NUMBER=jobcontrac2_.JOB_NUMBER 
left outer joinS CONTACTS contact3_ on jobcontrac2_.CONTACT_ID=contact3_.CONTACT_ID 
left outer join CUSTOMERS customer4_ on jobcontrac2_.CUST_CODE=customer4_.CUST_CODE 
left outer join CONTRACTS contract5_ on jobcontrac2_.CONTRACT_CODE=contract5_.CONTRACT_CODE 
left outer join JOB_CONTRACT_INVOICE jobcontrac6_ on jobcontrac2_.JOB_CONTRACT_ID=jobcontrac6_.JOB_CONTRACT_ID 
where to_char(customerjob0_.BU_NAME)='xYX01'
and (to_char(customerjob0_.BRANCH_NAME) like 'UX902')
and (to_char(customerjob0_.JOB_STATUS) in (2390 , 23400))
and to_char(customerjob0_.JOB_TYPE)='TEST' 
and (to_char(customerjob0_.JOB_FINISH_DATE) between '18-Sep-2008' and '18-Sep-2015');

实际上,我从日期的where子句中删除了to_char,它起了作用

您正在将字符串2014年2月12日与字符串20年9月18日[08 | 15]进行比较。因为二月!=它永远不会起作用。试着将其作为时间值进行比较?事实上,这只是一个例子,我写的是2014年2月12日。实际上,数据库中有许多日期并没有改变问题-2月永远不会在9月和9月之间,因为它们是字符串。EGOrecords你能试着详细说明我不擅长数据库吗?你正在相互比较字符串,这与数据库无关。您正在将日期转换为字符串,并将其与另一个字符串进行比较。如果你省略了铸造,它可能会起作用。
select distinct count(customerjob0_.JOB_NUMBER) as col_0_0_ from JOB_CUSTOMER customerjob0_
left outer join SERVICE_LOCATION serviceloc1_ on customerjob0_.SERVICE_LOCATION_CODE=serviceloc1_.SERVICE_LOCATION_CODE 
left outer join JOB_CONTRACTS jobcontrac2_ on customerjob0_.JOB_NUMBER=jobcontrac2_.JOB_NUMBER 
left outer joinS CONTACTS contact3_ on jobcontrac2_.CONTACT_ID=contact3_.CONTACT_ID 
left outer join CUSTOMERS customer4_ on jobcontrac2_.CUST_CODE=customer4_.CUST_CODE 
left outer join CONTRACTS contract5_ on jobcontrac2_.CONTRACT_CODE=contract5_.CONTRACT_CODE 
left outer join JOB_CONTRACT_INVOICE jobcontrac6_ on jobcontrac2_.JOB_CONTRACT_ID=jobcontrac6_.JOB_CONTRACT_ID 
where to_char(customerjob0_.BU_NAME)='xYX01'
and (to_char(customerjob0_.BRANCH_NAME) like 'UX902')
and (to_char(customerjob0_.JOB_STATUS) in (2390 , 23400))
and to_char(customerjob0_.JOB_TYPE)='TEST' 
and customerjob0_.JOB_FINISH_DATE between '18-Sep-2008' and '18-Sep-2015';