Mysql查询:
SELECT *
FROM `pet_info` LEFT JOIN
`lostpets`
ON `pet_info`.`id` = `lostpets`.`petid` LEFT JOIN
`pet_images`
ON `pet_info`.`id` = `pet_images`.`petid`
CASE WHEN `pet_info`.`pet_cat` = 2
THEN LEFT JOIN `cat_breeds`
ON `cat_breeds`.`id` = `pet_info`.`pet_breed`
WHEN `pet_info`.`pet_cat` = 1
THEN LEFT JOIN `dog_breeds`
ON `dog_breeds`.`id` = `pet_info`.`pet_breed`
WHERE `pet_info`.`pet_user_id` = 581
错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE WHEN `pet_info`.`pet_cat` = 2 THEN LEFT JOIN `cat_breeds` ON `cat_breeds`.`' at line 1
如何解决错误?哪里出错?请帮帮我。
答案 0 :(得分:3)
我想你打算:
SELECT *
FROM `pet_info` LEFT JOIN
`lostpets`
ON `pet_info`.`id` = `lostpets`.`petid` LEFT JOIN
`pet_images`
ON `pet_info`.`id` = `pet_images`.`petid` LEFT JOIN
`cat_breeds`
ON `cat_breeds`.`id` = `pet_info`.`pet_breed` AND
`pet_info`.`pet_cat` = 2 LEFT JOIN
`dog_breeds`
ON `dog_breeds`.`id` = `pet_info`.`pet_breed` AND
`pet_info`.`pet_cat` = 1
WHERE `pet_info`.`pet_user_id` = 581;
注意:
SELECT *
,您应该明确选择所需的列。不同的表具有相同名称的列。SELECT
中使用表达式来合并cat_breeds
和dog_breeds
中的列,例如COALESCE(cat_breeds.col1, dog_breeds.col1) as col1
。