简体   繁体   中英

MySql Query return wrong values in where clause

Using the following query I need to display only the records marked with the year "2014" in the month "02":

SELECT DISTINCT 
 c.sno,
 c.cCode,
 c.caseNumber,
 c.dateOriginalInstitution,
 c.date_remanded,
 c.plaintiff,
 c.respondant 
FROM
  cases c,
  chronologicallists ch 
WHERE (
 (YEAR(dateOriginalInstitution) = '2014' AND MONTH(dateOriginalInstitution) = '02') 
 OR 
 (YEAR(dateOfTransferInstituion) = '2014' AND MONTH(dateOfTransferInstituion) = '02') 
 OR 
 (YEAR(date_restored) = '2014' AND MONTH(date_restored) = '02')
) 
AND c.inTheCourt = '578' AND c.sno = ch.caseSno 

在此处输入图片说明

I'm still getting 2011 and 2012 in the result set?

Please help me with this.

It is because of your OR condition.

You might be having year value as 2014 in either dateOfTransferInstituion or date_restored columns but not in dateOriginalInstitution column

There are 3 columns responsible for this dateOriginalInstitution , dateOfTransferInstituion , dateOfTransferInstituion that you should check

Use AND instead of OR and mind to display dateOfTransferInstituion for you to know if they passed your WHERE condition.

WHERE (
 (YEAR(dateOriginalInstitution) = '2014' AND MONTH(dateOriginalInstitution) = '02') 
 AND 
 (YEAR(dateOfTransferInstituion) = '2014' AND MONTH(dateOfTransferInstituion) = '02') 
 AND 
 (YEAR(date_restored) = '2014' AND MONTH(date_restored) = '02')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM