On Wednesday 22 January 2003 19:39, Bill Rausch wrote:

> I've inherited a busted database and need to clean it up.  I can't
> figure out how to do something which seemingly should be simple.
>
> For example, I've got two tables:
>
> Table One
> id int primary key auto_increment not null
> ...other data
>
> Table Two
> id int primary key auto_increment not null
> ...other data
>
> I'd like to delete all of the records from Table Two which have an id
> that is not found in Table One.
>
> I could do it programatically in C or PHP or ... but is there some
> kind of SQL statement than can do the job?

You can do it in version 4.0 and up, like

DELETE t2 FROM t2 LEFT JOIN t1 ON t2.id=t1.id WHERE t1.id IS NULL;

or since 4.0.2 like

DELETE FROM t2 USING t2 LEFT JOIN t1 ON t2.id=t1.id WHERE t1.id IS NULL;

for more info check the following section of the manual:
        http://www.mysql.com/doc/en/DELETE.html


-- 
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





---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to