Jamie McAllister Jamie McAllister - 5 months ago 9
SQL Question

How to INNER JOIN multiple tables

I'm a little confused now.

I have 4 tables that look like so:

dog
ID
<numerous fields>
vetID

client
ID
<again, numerous fields i don't think are relevant>

vet
ID
<More irrelevant fields>

dogclient
ID
DogID
ClientID


I'm trying to get all the information from all these tables for a specific dog's ID. I tried this query in a PDO prepared statement

SELECT dog.*, client.*, vet.*
FROM dogclient
INNER JOIN client ON dogClient.ClientID = client.ID
INNER JOIN dog ON dogclient.DogID = dog.ID
INNER JOIN dog.vetID = vet.ID
WHERE dogclient.dogID = :id


the result was a syntax error in the query at:

'.ID WHERE dogclient.dogID = '4''


Can anyone help me fix this?

Answer
SELECT dog.*, client.*, vet.* FROM dogclient 
    INNER JOIN client ON dogClient.ClientID = client.ID 
    INNER JOIN dog ON dogclient.DogID = dog.ID 
    INNER JOIN vet ON dog.vetID = vet.ID 
    WHERE dogclient.dogID = :id

you were missing the vet table in the last inner join

Comments