JOIN语句错误时的Mysql案例

JOIN语句错误时的Mysql案例,mysql,sql,Mysql,Sql,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

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 
如何解决错误?哪里有错误?请帮帮我。

我想你打算:

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_品种和dog_品种的列,例如COALESCEcat_品种.col1、dog_品种.col1作为col1。 我认为你打算:

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_品种和dog_品种的列,例如COALESCEcat_品种.col1、dog_品种.col1作为col1。
这根本不是案例所做的。它是一个标量表达式,返回一个值,而不是FROM子句中的条件构造。它是一个返回值的标量表达式,而不是FROM子句中的条件构造。1064-SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,了解在第11064行“pet_info.pet_cat=1,其中pet_info.pet_user_id=581 LIMIT 0,30”附近使用的正确语法-您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以了解在第1行“pet_info.pet_cat=1,其中pet_info.pet_user_id=581 LIMIT 0,30”附近使用的正确语法