Nice that this came up when it did.. I'm currently struggling with a
three-table join.
Table1.PK = Table2.FK1
Table3.PK = Table2.FK2
My last effort looks something like:
SELECT SomeStuff
FROM Table1 AS T1
LEFT JOIN Table2 AS T2 ON T1.PK = T2.FK1
RIGHT JOIN Table3 AS T3 ON T2.FK2 = T3.PK
WHERE T1.PK=999
I want it to return 1 row, but it's returning as many rows as there are
in Table3. Where am I goofing?
TIA,
Tab
mysql
-----Original Message-----
From: Michael T. Babcock [mailto:mbabcock@stripped]
Sent: Wednesday, January 15, 2003 8:30 AM
To: Josh L Bernardini
Cc: mysql@stripped
Subject: Re: three table join
I've wanted to post this query example a few times (and I hope I got it
right; mornings aren't my best time) ... multiple JOINs:
SELECT stuff
FROM table1
LEFT JOIN table2
ON table1.fk = table2.pk
LEFT JOIN table3
ON table2.fk = table3.pk
WHERE other_conditions
...
You can repeat that as many levels as you want (performance depends on
indexing and the optimizer). You need to think in terms of what would
be equal to what between tables in the correct result row. So if you
would do a secondary sub-select of "SELECT fk from table2 where ..."
then you end up with a left join like above.