Jump to content

Join regardless of match?


Kryptix

Recommended Posts

Say I have two tables...

 

staff

staff_addresses

 

If both tables don't have a match I want to delete the other tables record.

 

DELETE `staff`.*, `staff_address`.* FROM `staff` JOIN `staff_addresses` ON (`staff`.`id` = `staff_addresses`.`staff_id`) WHERE `staff`.`id` IS NULL OR `staff_addresses`.`id` IS NULL;

 

I can use a LEFT or RIGHT join if I know which won't have an entry, but is it possible to do a join anyway regardless if it has a match?

Link to comment
Share on other sites

You know how LEFT or RIGHT JOIN will join regardless if there's a match, I want it to join without having to define LEFT or RIGHT if that makes sense...

 

If I'm joining the above, regardless if there's an entry in address OR staff, it joins anyway (displays the result)

Link to comment
Share on other sites

You could try the following:

DELETE staff.*, staff_address.*
FROM staff, staff_address 
WHERE (staff.id NOT IN (SELECT staff_id FROM staff_address)) 
OR (staff_address.staff_id NOT IN (SELECT id from Staff)

I doubt that will work though, and if it does I'm not sure what it will actualy perform, I have never tried to simultaniuosly delte from two tables whilest using a cross reffence check on just those tables.  I personaly would use two DELETE queries - one for each table - in this case.

Link to comment
Share on other sites

Hi

 

Seems what you need is a FULL OUTER JOIN, which mysql doesn't support.

 

You could possibly emulate it using a LEFT OUTER JOIN and UNIONing the results with a RIGHT OUTER JOIN.

 

However to be honest I would be a bit fearful of doing something like this on a bulk delete.

 

All the best

 

Keith

Link to comment
Share on other sites

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.