Re: Delete with subquery deleting all records - Mailing list pgsql-general

From Alban Hertroys
Subject Re: Delete with subquery deleting all records
Date
Msg-id 46554E8F.3050401@magproductions.nl
Whole thread Raw
In response to Delete with subquery deleting all records  (Francisco Reyes <lists@stringsutils.com>)
List pgsql-general
Francisco Reyes wrote:
> When I try to run:
> delete from export_messages where export_id in
> (SELECT distinct export_messages.export_id as id
> FROM export_messages
> LEFT OUTER JOIN exports ON (export_messages.export_id = exports.export_id)
> );

Why not use EXISTS?

DELETE FROM export_messages WHERE NOT EXISTS (
    SELECT 1
      FROM exports
     WHERE exports.export_id = export_messages.export_id
)

I suppose you run those queries in a transaction block, right?

--
Alban Hertroys
alban@magproductions.nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
   7500 AK Enschede

// Integrate Your World //

pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: OIDs - file objects, are damaged by PostgreSQL.
Next
From: Philippe Amelant
Date:
Subject: Re: Tsearch2: upgrading from postgres 8.1.x to 8.2.x