Re: update only if single row

From: "Moray McConnachie" <moray(dot)mcconnachie(at)computing-services(dot)oxford(dot)ac(dot)uk>
To: "PGSQL SQL" <pgsql-sql(at)postgresql(dot)org>, "Frank Bax" <fbax(at)execulink(dot)com>
Subject: Re: update only if single row
Date: 2000-04-07 08:46:56
Message-ID: 006001bfa06d$d45f5480$760e01a3@oucs.ox.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Moray McConnachie 2000-04-07 09:36:52 Maxima per row
Previous Message Moray McConnachie 2000-04-07 08:39:29 Re: update only if single row