How to join to delete

Lists: pgsql-general
From: "Chris Velevitch" <chris(dot)velevitch(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: How to join to delete
Date: 2006-05-02 06:53:58
Message-ID: b0a3bf780605012353u7799e90av2a2bb71b0eaec41e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I'm using pg 7.4.5 on Win XP SP1.

I want to delete rows from a table B, but need to join it to another
table A in order to select the rows in B to be deleted.

Eg. Delete from A join B on (A.bk = B.bk) where A.ak = x;

but only delete rows from B.

How do I acheive this?

Chris
--
Chris Velevitch
Manager - Sydney Flash Platform Developers Group
www.flashdev.org.au


From: Noel Faux <noel(dot)faux(at)med(dot)monash(dot)edu(dot)au>
To: Chris Velevitch <chris(dot)velevitch(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to join to delete
Date: 2006-05-02 07:03:09
Message-ID: 4457042D.2040307@med.monash.edu.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi Chris,

I think the best bet would be to use a nested select:

Delete from A WHERE A.bk = (SELECT bk FROM B) where A.ak = x;

Cheers
Noel

Chris Velevitch wrote:
> I'm using pg 7.4.5 on Win XP SP1.
>
> I want to delete rows from a table B, but need to join it to another
> table A in order to select the rows in B to be deleted.
>
> Eg. Delete from A join B on (A.bk = B.bk) where A.ak = x;
>
> but only delete rows from B.
>
> How do I acheive this?
>
>
> Chris
> --
> Chris Velevitch
> Manager - Sydney Flash Platform Developers Group
> www.flashdev.org.au
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings

Attachment Content-Type Size
noel.faux.vcf text/x-vcard 260 bytes

From: Richard Huxton <dev(at)archonet(dot)com>
To: Chris Velevitch <chris(dot)velevitch(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to join to delete
Date: 2006-05-02 08:18:30
Message-ID: 445715D6.7030706@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Chris Velevitch wrote:
> I'm using pg 7.4.5 on Win XP SP1.

The first official release for Windows was 8.0, and there have been
several bugfixes to 7.4.x since then. Upgrade as soon as is practical.

> I want to delete rows from a table B, but need to join it to another
> table A in order to select the rows in B to be deleted.
>
> Eg. Delete from A join B on (A.bk = B.bk) where A.ak = x;
>
> but only delete rows from B.

Perhaps something like:
DELETE FROM B WHERE bk IN (SELECT ak FROM A WHERE ak2 = x)

Or maybe rephrase it as EXISTS for 7.4

--
Richard Huxton
Archonet Ltd


From: "Chris Velevitch" <chris(dot)velevitch(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to join to delete
Date: 2006-05-02 13:00:55
Message-ID: b0a3bf780605020600r36444c6ay4599358b50e50012@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 5/2/06, Richard Huxton <dev(at)archonet(dot)com> wrote:
> Chris Velevitch wrote:
> > I'm using pg 7.4.5 on Win XP SP1.
>
> The first official release for Windows was 8.0, and there have been

I'm the cygwin version of pg for testing.

> several bugfixes to 7.4.x since then. Upgrade as soon as is practical.

I'm deploying to a shared hosted database and they are currently
supporting 7.4.x.

Chris
--
Chris Velevitch
Manager - Sydney Flash Platform Developers Group
www.flashdev.org.au