tHud Posted October 16, 2010 Share Posted October 16, 2010 Hello (I think this is more of a php question than MySQL - sorry if I have got it wrong.) I need to know if items have been orphaned (because they lack a cat_id that actually exists). I have two MySQL tables that each have fields 'cat_id'. I have a Category table... cat_id / category 1 / fruit 2 / veg 3 / tools I have another product table cat_id / product 1 / apple 1 / Pear 2 / carrot 2 / cabbage 3 / screwdriver 1 / peach 8 / orphan I have entered the data into two arrays... $categories[] $products[] I have tried a number of combinations of foreach loops to try to determine if the products(cat_id) doesn't match the categories(cat_id) but I seem to be going around in circles. Is there perhaps a function that is available to do this? Or maybe just a straight thinking person that can point out my ineptitude? Thank you for any input. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 16, 2010 Share Posted October 16, 2010 May need some tweaking, but give this a shot. SELECT * FROM product WHERE cat_id NOT IN (SELECT DISTINCT cat_id FROM category) Quote Link to comment Share on other sites More sharing options...
tHud Posted October 18, 2010 Author Share Posted October 18, 2010 Amazing - I would never have thought of that. THANK YOU Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 18, 2010 Share Posted October 18, 2010 No problem at all. Quote Link to comment 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.