MySql Query在where子句中返回错误的值

时间:2014-02-24 09:30:04

标签: php mysql sql date recordset

使用以下查询我只需要在月份“02”中显示标有年份“2014”的记录:

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

我仍然在结果集中获得2011年和2012年?

请帮助我。

2 个答案:

答案 0 :(得分:5)

这是因为你的OR条件。

year2014列中的dateOfTransferInstituiondate_restored可能不是dateOriginalInstitution

答案 1 :(得分:0)

您需要检查的dateOriginalInstitutiondateOfTransferInstituiondateOfTransferInstituion共有3列

使用AND而不是OR,请注意显示dateOfTransferInstituion,以便了解 他们通过了WHERE条件。

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