2017-04-16 88 views
0

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 

爲什麼錯誤出現,請幫助我。 Gow可以解決這個錯誤嗎?

+3

'LEFT JOIN ON'。 - 一些基本的格式可能會有所幫助。 – Strawberry

+0

你能幫我寫答案嗎? –

+1

不是。錯誤太多了。 – Strawberry

回答

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