iLifeNetworks Posted April 13, 2010 Share Posted April 13, 2010 I have been working at this for some time. I am attempting to delete records on multiple tables within same database where the vendor_id matches the vendor ID for a given state ID on one table. For example. Tables: vendors (main table where we match vendor_id field from) vendors_to_category (contains vendor_id) vendors_service_areas (contains_vendor_id) So, how do we delete any records from the vendors_to_category table as well as vendors_service_areas table if the vendor_id doesn't exist in the vendors table? i have tried some different inner joins, left joins, cross joins, with no success... delete from vendors.*, vendors_photo_gallery.*, vendors_service_areas.*, vendors_to_category.* USING vendors v INNER JOIN vendors INNER JOIN vendors_photo_gallery INNER JOIN vendors_service_areasINNER JOIN vendors_to_category where v.vendor_id = vendors_photo_gallery.vendor_id or v.vendor_id = vendors_service_areas.vendor_id or v.vendor_id = vendors_to_category.vendor_id Quote Link to comment https://forums.phpfreaks.com/topic/198332-tricky-deleting-statements/ Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 Why not run 3 simple delete queries? Quote Link to comment https://forums.phpfreaks.com/topic/198332-tricky-deleting-statements/#findComment-1040664 Share on other sites More sharing options...
andrewgauger Posted April 13, 2010 Share Posted April 13, 2010 delete from vendors_to_category where vendor_id NOT IN(SELECT vendor_id from vendors); This might scope to ...vendors_to_category, vendors_service_area where... but I don't have a practical way to test it. Go ahead and try it out. Quote Link to comment https://forums.phpfreaks.com/topic/198332-tricky-deleting-statements/#findComment-1040671 Share on other sites More sharing options...
Mchl Posted April 13, 2010 Share Posted April 13, 2010 I think you want something like this: DELETE vendors.*, vendors_photo_gallery.*, vendors_service_areas.*, vendors_to_category.* FROM vendors v CROSS JOIN vendors_photo_gallery USING(vendor_id) CROSS JOIN vendors_service_areas USING(vendor_id) CROSS JOIN vendors_to_category USING(vendor_id) Quote Link to comment https://forums.phpfreaks.com/topic/198332-tricky-deleting-statements/#findComment-1040802 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.