pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Moray McConnachie <moray.mcconnachie@computing-services.oxford.ac.uk>
To: PGSQL SQL <pgsql-sql@postgresql.org>
To: Frank Bax <fbax@execulink.com>
Subject: Re: update only if single row
Date: Fri, 7 Apr 2000 09:46:56 +0100
Message-ID: <006001bfa06d$d45f5480$760e01a3@oucs.ox.ac.uk> (raw)
References: <3.0.6.32.20000406132216.008a8420@execulink.com>

> 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





view thread (8+ messages)  latest in thread

Message-ID: <006001bfa06d$d45f5480$760e01a3@oucs.ox.ac.uk>
Permalink:  ../006001bfa06d$d45f5480$760e01a3@oucs.ox.ac.uk/
Also on:    postgresql.org/message-id/006001bfa06d$d45f5480$760e01a3@oucs.ox.ac.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: moray.mcconnachie@computing-services.oxford.ac.uk, fbax@execulink.com
  Subject: Re: update only if single row
  In-Reply-To: <006001bfa06d$d45f5480$760e01a3@oucs.ox.ac.uk>
  References: <3.0.6.32.20000406132216.008a8420@execulink.com> <006001bfa06d$d45f5480$760e01a3@oucs.ox.ac.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox