Re: delete where not in another table

From: Igor Neyman <ineyman(at)perceptron(dot)com>
To: "Campbell, Lance" <lance(at)illinois(dot)edu>, "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: delete where not in another table
Date: 2013-07-09 20:36:56
Message-ID: A76B25F2823E954C9E45E32FA49D70EC1BBDB24E@mail.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-owner(at)postgresql(dot)org] On Behalf Of Campbell, Lance
Sent: Tuesday, July 09, 2013 3:25 PM
To: pgsql-sql(at)postgresql(dot)org
Subject: [SQL] delete where not in another table

 
DELETE FROM T1 WHERE T1.user_id NOT IN (SELECT user_id FROM T2 WHERE T2.user_id=T1.user_id);
 

Thanks,
 
Lance Campbell
Software Architect
Web Services at Public Affairs
217-333-0382

  --
Try:

DELETE FROM t1
USING t2
WHERE t1.user_id != t2.user_id;

Test it before running on production db.

Regards,
Igor Neyman
 

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Huan Ruan 2013-07-11 05:17:53 DateDiff() function
Previous Message Campbell, Lance 2013-07-09 19:25:14 delete where not in another table