pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jasen Betts <jasen@xnet.co.nz>
To: pgsql-sql@postgresql.org
Subject: Re: Setting a default value for a select statement without results
Date: 6 Feb 2013 04:07:37 GMT
Message-ID: <kesku9$ih0$2@gonzo.reversiblemaps.ath.cx> (raw)
References: <CAAY=A7_cM2ZrO9oD=_pbuX6vdNiVST3iXLfRDZ5_YHASoABZBg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

On 2013-02-06, JORGE MALDONADO <jorgemal1960@gmail.com> wrote:
> --f46d0401fb2fcb805e04d50354b1
> Content-Type: text/plain; charset=ISO-8859-1
>
> I have an UPDATE query with the following general structure:
>
> UPDATE table1 SET (SELECT field FROM table2 WHERE conditions ORDER BY
> order_field LIMIT 1)

assuming you mean this, and you are happy with its performance.
 
 UPDATE table1 SET field1 = (SELECT field FROM table2 WHERE conditions ORDER BY
 order_field LIMIT 1)
 
 
> Is it possible to assign a default value in case no results are returned by
> the SELECT statement?

use coalesce.

 UPDATE table1 SET field1 = coalesce( (SELECT field FROM table2 WHERE conditions ORDER BY
 order_field LIMIT 1) , default_value )




-- 
⚂⚃ 100% natural



-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



view thread (5+ messages)  latest in thread

Message-ID: <kesku9$ih0$2@gonzo.reversiblemaps.ath.cx>
Permalink:  ../kesku9$ih0$2@gonzo.reversiblemaps.ath.cx/
Also on:    postgresql.org/message-id/kesku9$ih0$2@gonzo.reversiblemaps.ath.cx

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jasen@xnet.co.nz
  Subject: Re: Setting a default value for a select statement without results
  In-Reply-To: <kesku9$ih0$2@gonzo.reversiblemaps.ath.cx>

* 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