如何获取左连接mySQL表的id? [关闭]

Like this my mySQL request is working well:

 <?php
       $id = 12;

        $pdo = $db->prepare("
        SELECT *,
        horse.id AS h
        FROM dog 
        LEFT JOIN cat ON dog.name=cat.name
        LEFT JOIN horse ON dog.name=horse.name
        WHERE animal = ?");
        $pdo->execute(array($id));
        $animals = $pdo->fetchAll();

        foreach ($animals as $row) {
            echo $row["h"];
        }
        ?>

But if I want to get the dog.id...

 <?php

        $id = 12;

        $pdo = $db->prepare("
        SELECT *,
        horse.id AS h
        dog.id AS d
        FROM dog 
        LEFT JOIN cat ON dog.name=cat.name
        LEFT JOIN horse ON dog.name=horse.name
        WHERE animal = ?");
        $pdo->execute(array($id));
        $animals = $pdo->fetchAll();

        foreach ($animals as $row) {
            echo $row["h"];
            echo $row["d"];
        }
        ?>

...there is an error:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'dog.id AS d FROM dog LEFT JOIN cat ON dog.name=cat.name' at line 3 in /myproject.php:31 Stack trace: #0 /myproject.php(31): PDOStatement->execute(Array) #1 {main} thrown in /myproject.php on line 31

Based on your data and your error message, you missed one comma in your query after horse.id AS h it should be

SELECT *,
 horse.id AS h,
 dog.id AS d
FROM dog 
LEFT JOIN cat ON dog.name=cat.name
LEFT JOIN horse ON dog.name=horse.name

You're missing a comma:

    SELECT *,
    horse.id AS h  -- HERE
    dog.id AS d

Should be:

    SELECT *,
    horse.id AS h,
    dog.id AS d