I’m working with a legacy database that uses a three column key for products. I want to select all products that have a status of ‘A’ or that have a matching record in a second table. If it were a single column primary key (like ‘id’), I would do it this way:
SELECT * FROM `product`
WHERE `status` = 'A'
OR `id` IN (SELECT `foreign_key` FROM `table2`)
I can’t figure out how to do the IN-clause subselect with three keys though. I suppose I can concatenate the keys together and compare the strings, but that seems horribly inefficient. Is there a way to do this without concatenation?
You can LEFT JOIN table product and table2 on the composite key, then
status= 'A' ORtable2.idIS NOT NULLA LEFT [OUTER] JOIN can be faster than an equivalent subquery because the server might be able to optimize it better