MYSQL加入错误。怎么解决?

时间:2017-04-16 16:34:06

标签: mysql

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` LEFT JOIN ON `pet_info`.`pet_cat` CASE WHEN `pet_info`.`pet_cat` = 1 THEN `dog_breeds`.`id` WHEN `pet_info`.`pet_cat` = 2 THEN `cat_breeds`.`id` END 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 'ON `pet_info`.`pet_cat` CASE WHEN `pet_info`.`pet_cat` = 1 THEN `do' at line 7

为什么出现错误请帮助我。我可以解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

以下是查询的格式化版本,突出显示了一些错误。

SELECT * 
  FROM pet_info p
  LEFT 
  JOIN lostpets l
    ON p.id = l.petid 
  LEFT 
  JOIN pet_images i
    ON p.id = i.petid 
  LEFT 
  JOIN  !! ERROR 1
    ON p.pet_cat !! ERROR 2
  CASE WHEN p.pet_cat = 1 
       THEN d.id !! ERROR 3
       WHEN p.pet_cat = 2 
       THEN c.id END !! ERROR 4
 WHERE p.pet_user_id = 581