mds1256 Posted March 17, 2013 Share Posted March 17, 2013 Hi This has been driving me crazy, I have the following tables: Parts: Part_ID p1 p2 p3 p4 p5 Store: Store_ID s1 s2 s3 s4 Stocks: Part_ID | Store_ID p1 s1 p2 s1 p3 s1 p4 s1 p5 s1 p1 s2 p2 s2 Any what I am trying to do is to get a list of ALL store_IDs which do not stock all products. So I need to check inside the Stocks table to see if for each Store_ID that each instance of Product_ID exists for that Store. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/275764-sql-statement-question/ Share on other sites More sharing options...
DaveyK Posted March 18, 2013 Share Posted March 18, 2013 are p1, p2, p3 and such ids of rows or the names of columns? Quote Link to comment https://forums.phpfreaks.com/topic/275764-sql-statement-question/#findComment-1419324 Share on other sites More sharing options...
Solution Psycho Posted March 18, 2013 Solution Share Posted March 18, 2013 Maybe not the most efficient, but this works This will return just the store IDs that do not carry all the parts SELECT s.store_id FROM store AS s JOIN parts AS p LEFT JOIN `stocks` AS st ON s.store_id = st.store_id AND p.part_id = st.part_id WHERE st.store_id IS NULL GROUP BY s.store_id Or, if you want to include the list of parts each store does not carry, then include the part_id in the SELECT clause and remove the GROUP BY. SELECT s.store_id, p.part_id FROM store AS s JOIN parts AS p LEFT JOIN `stocks` AS st ON s.store_id = st.store_id AND p.part_id = st.part_id WHERE st.store_id IS NULL -- GROUP BY s.store_id Quote Link to comment https://forums.phpfreaks.com/topic/275764-sql-statement-question/#findComment-1419336 Share on other sites More sharing options...
mds1256 Posted March 18, 2013 Author Share Posted March 18, 2013 Works Perfect!!! Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/275764-sql-statement-question/#findComment-1419382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.