Abdul Rahman
Abdul Rahman

Reputation: 1705

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 

enter image description here

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

Please help me with this.

Upvotes: 0

Views: 350

Answers (2)

Jhonathan H.
Jhonathan H.

Reputation: 2713

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

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

Reputation: 24002

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

Upvotes: 5

Related Questions