Click to See Complete Forum and Search --> : Query question - Need results from 3 tables


dpkyte
10-18-2007, 12:05 AM
I have 3 tables.

Basically, I need to find all the records in table 3, that are not found in tables 1 or 2.


Table1.Table1_id
Table2.Table2_id
Table3.Table3.id

I actually read through tables 1 and 2 and stored their respective table id's in an array $table_ids[]. So, I believe I can do some sort of outer join to find out what I dont want using that array.

To take a step further. I have a 4th table "products", that I would like to get information off of. I could either include that table or just read through the results set and get that information if easier.

Any thoughts feel free to pass them on.

Thanks in advance.

DPK

cridley
10-18-2007, 10:43 AM
SELECT * FROM Table3 WHERE id NOT IN (SELECT id FROM Table1) AND id NOT IN (SELECT id FROM Table2)

Will display all records in table3 which have an id not found in either table1 or table2. This might be slow on large sets of data.

Not sure what you mean with the products table though. You want to get all the products for IDs returned in the query above?

Then
SELECT * FROM Table3 LEFT JOIN Products ON Table3.id = Products.id WHERE id NOT IN (SELECT id FROM Table1) AND id NOT IN (SELECT id FROM Table2)

dpkyte
10-19-2007, 09:54 AM
Thanks much, seemed to do the trick.