Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/198332-tricky-deleting-statements/
Share on other sites

 

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.

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.