Re: DELETE with JOIN - Mailing list pgsql-sql

From Mark Roberts
Subject Re: DELETE with JOIN
Date
Msg-id 1218140820.28304.67.camel@localhost
Whole thread Raw
In response to DELETE with JOIN  (felix@crowfix.com)
List pgsql-sql
On Thu, 2008-08-07 at 09:14 -0700, felix@crowfix.com wrote:
>    DELETE FROM a WHERE a.b_id = b.id AND b.second_id = ?
> 

This should work for your needs:
delete from a
using b
where a.id = b.id -- join criteria
and b.second_id = ?

> I have tried to do this before and always found a way, usually
>     DELETE FROM a WHERE a.b_id IN (SELECT id FROM b WHERE second_id
> = ?)

This is akin to:
delete from a
where (a.key1, a.key2, a.key3) in (select key1, key2, key3 from b)

I use this every day for millions of rows per delete and it works just
fine and in a very reasonable time period.

-Mark



pgsql-sql by date:

Previous
From: Frank Bax
Date:
Subject: Re: DELETE with JOIN
Next
From: felix@crowfix.com
Date:
Subject: Re: DELETE with JOIN