I have three tables: Table1, Table2, and Lookup. Table1 and Table2 have no columns in common. Lookup is for tagging: it has two columns that match Table1 ids with Table2 ids.
I am trying to design a query where I can take parameters to get an array of rows from Table1, then using all the ids in my result, get an array of all the corresponding Lookup rows, then use that to get an array of all the corresponding tags in Table2.
The hard way would be to do the first query, programmatically get all of the unique ids, then select from Lookup based off those, etc. But that seems really inefficient.
I'm using PHP, PDO, and PostgreSQL. What can I do?
Try this:
SELECT *
FROM Lookup
LEFT JOIN Table1 ON Table1.id=Lookup.Table1_id
LEFT JOIN Table2 ON Table2.id=Lookup.Table2_id