Dan Jones <[EMAIL PROTECTED]> wrote:
> How do I update a table to remove orphaned references to a second
> table?  I've deleted rows in the second table, which has a unique
> auto_increment key.  The first table now has references to keys that no
> longer exist.  I need to update the first table, setting the value to
> NULL where the referenced key no longer exists.  Something like:
> 
> UPDATE table1 SET table2ID = NULL WHERE table1.table2ID NOT FOUND IN
> table2.ID;
> 
> The NOT FOUND IN isn't SQL, of course, but I'm not sure what should go
> there.

If you have MySQL server version 4.0.4 or newer, you can do something like:

UPDATE table1 LEFT JOIN table2 ON table1.table2ID=table2.ID SET table1.table2ID=NULL 
WHERE table2.ID IS NULL.


-- 
For technical support contracts, goto https://order.mysql.com/?ref=ensita
This email is sponsored by Ensita.net http://www.ensita.net/
   __  ___     ___ ____  __
  /  |/  /_ __/ __/ __ \/ /    Victoria Reznichenko
 / /|_/ / // /\ \/ /_/ / /__   [EMAIL PROTECTED]
/_/  /_/\_, /___/\___\_\___/   MySQL AB / Ensita.net
       <___/   www.mysql.com





-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to