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. Link to comment https://forums.phpfreaks.com/topic/216032-looking-for-an-orphan-comparing-arrays/ 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) Link to comment https://forums.phpfreaks.com/topic/216032-looking-for-an-orphan-comparing-arrays/#findComment-1122825 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 Link to comment https://forums.phpfreaks.com/topic/216032-looking-for-an-orphan-comparing-arrays/#findComment-1123475 Share on other sites More sharing options...
Pikachu2000 Posted October 18, 2010 Share Posted October 18, 2010 No problem at all. Link to comment https://forums.phpfreaks.com/topic/216032-looking-for-an-orphan-comparing-arrays/#findComment-1123479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.