Mysql CASE WHEN JOIN Statement Error

前端 未结 1 671
再見小時候
再見小時候 2021-01-28 02:34

Mysql Query:

SELECT *
FROM `pet_info` LEFT JOIN
     `lostpets`
     ON `pet_info`.`id` = `lostpets`.`petid` LEFT JOIN
     `pet_images`
     ON `pet_info`.`id`          


        
1条回答
  •  清歌不尽
    2021-01-28 03:19

    I think you intend:

    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;
    

    Notes:

    • With a query like this, you should not use SELECT *, you should explicitly choose the columns you want. The different tables have columns with the same name.
    • You should use column aliases. I didn't put these into the query, but they are a good idea.
    • In a real query, you would have expressions in the SELECT to combine columns from cat_breeds and dog_breeds, such as COALESCE(cat_breeds.col1, dog_breeds.col1) as col1.

    0 讨论(0)
提交回复
热议问题