Re: DELETE with JOIN

From: Mark Roberts <mailing_lists(at)pandapocket(dot)com>
To: felix(at)crowfix(dot)com
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: DELETE with JOIN
Date: 2008-08-07 20:27:00
Message-ID: 1218140820.28304.67.camel@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Thu, 2008-08-07 at 09:14 -0700, felix(at)crowfix(dot)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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message felix 2008-08-07 21:35:46 Re: DELETE with JOIN
Previous Message Frank Bax 2008-08-07 20:01:29 Re: DELETE with JOIN