在将日期条件添加到where子句时查询不给出结果

时间:2015-09-21 02:23:30

标签: mysql sql hibernate

查询下面返回正确结果如果没有添加两个日期之间的where子句,否则它返回零,这是我创建的hibernate查询的sql等价物。 在我的数据库日期保存为例如: - 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');

1 个答案:

答案 0 :(得分:0)

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';

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