我无法弄清楚这个SQL语句中的任何错误。任何人都可以请帮助我哪里有错误,我该如何解决这个问题?提前谢谢,对不起拼写或语法错误。
SQL:
SELECT count(*) as `total` FROM `pet_info` LEFT JOIN `lostpets` ON `lostpets`.`petid` = `pet_info`.`id` WHERE `lostpets`.`reunited`='No' AND (CASE WHEN `pet_info`.`pet_aggg` = 'birth' THEN `pet_info`.`pet_birthdate` <= '2011-05-12' WHEN `pet_info`.`pet_aggg` = 'age' THEN `pet_info`.`pet_age` >= '7');
答案 0 :(得分:1)
只需添加&#39;结束&#39;在您的选择结束时:那么pet_info
。pet_age
&gt; =&#39; 7&#39;端);
答案 1 :(得分:1)
这是您的查询:
SELECT count(*) as `total`
FROM `pet_info` LEFT JOIN
`lostpets`
ON `lostpets`.`petid` = `pet_info`.`id`
WHERE `lostpets`.`reunited`='No' AND
(CASE WHEN `pet_info`.`pet_aggg` = 'birth'
THEN `pet_info`.`pet_birthdate` <= '2011-05-12'
WHEN `pet_info`.`pet_aggg` = 'age'
THEN `pet_info`.`pet_age` >= '7'
);
明显的语法问题是缺少END
。但是你有其他问题。查询应该如下所示:
SELECT count(*) as `total`
FROM `pet_info` pi JOIN
`lostpets` l
ON l.`petid` = pi.`id`
WHERE l.`reunited`= 'No' AND
((pi.pet_aggg = 'birth' AND pi.pet_birthdate <= '2011-05-12') OR
(pi.pet_aggg = 'age' AND pi.pet_age >= 7)
);
注意:
WHERE
子句会将LEFT JOIN
变为INNER JOIN
。CASE
在WHERE
条款中往往令人困惑。只需使用基本的布尔逻辑。