SELECT updateDate, id,cSsn,cLname,cFname,cDOB,cDOD,cCity,cState,cHospital,cCurstatus,cMmrcashworker,cTelephone FROM med_patient WHERE cCurstatus!='completed' AND cMmrcashworker = '2' AND cHospital = '1234' OR cHospital1 = '1234' OR cHospital2 ='1234' AND updateDate between '1/30/2010' and '1/28/2010' order by id desc'
Output:
updateDate cHospital cHospital1 开发者_如何学JAVA cHospital2
01/15/2010 1234
01/15/2010 1234
But acutally to my knowledge my query must return an empty row.
Where is the mistake in the query?
try adding some parentheses to your cHospital condition, otherwise the or will not be what you expect:
cCurstatus!='completed' AND cMmrcashworker = '2' AND
(cHospital = '1234' OR cHospital1 = '1234' OR cHospital2 ='1234') AND
updateDate between '1/30/2010' and '1/28/2010' order by id desc'
also:
... updateDate between '1/30/2010' and '1/28/2010' ...
^ that is doing a string compare rather than a date comparison, try changing to:
... updateDate between '2010-01-30' and '2010-01-28' ...
My problem is fixed. As per jspcal's guidance, I changed my datetype to datetime.
Now I get the expected output.
精彩评论