Re: update only if single row - Mailing list pgsql-sql

From Moray McConnachie
Subject Re: update only if single row
Date
Msg-id 006001bfa06d$d45f5480$760e01a3@oucs.ox.ac.uk
Whole thread Raw
In response to update only if single row  (Frank Bax <fbax@execulink.com>)
List pgsql-sql
> select * from contact where email ~* 'rvro';
> if I get a single row in the result then I enter:
> update contact set bounce=1 where  email ~* 'rvro';
>
> Can I combine this into a single SQL statement with the following
> requirements:
> 1) the update is only performed if a single row is selected
> 2) I only enter the selection string ('rvro' in this case) once in the

If you have some kind of id field in the table, you can do it like this, or
you can use the oids, but I don't know how to write that :->
I'm not quite sure that the aliasing works properly in this query either -
does the EXISTS clause pick up the a alias?

UPDATE contact a SET a.bounce=1 WHERE a.email ~* 'rvro' AND NOT EXISTS
(SELECT id FROM contact b WHERE b.email ~*'rvro' AND a.id !=b.id);

Yours,
Moray




pgsql-sql by date:

Previous
From: "Moray McConnachie"
Date:
Subject: Re: update only if single row
Next
From: "tjk@tksoft.com"
Date:
Subject: Re: update only if single row