Re: Setting a default value for a select statement without results - Mailing list pgsql-sql

From Jasen Betts
Subject Re: Setting a default value for a select statement without results
Date
Msg-id kesku9$ih0$2@gonzo.reversiblemaps.ath.cx
Whole thread Raw
In response to Setting a default value for a select statement without results  (JORGE MALDONADO <jorgemal1960@gmail.com>)
Responses Re: Setting a default value for a select statement without results
List pgsql-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
WHEREconditions ORDER BYorder_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 BYorder_field LIMIT 1) ,
default_value)
 




-- 
⚂⚃ 100% natural




pgsql-sql by date:

Previous
From: Wayne Cuddy
Date:
Subject: index scan vs bitmap index scan
Next
From: Anton Gavazuk
Date:
Subject: Re: Setting a default value for a select statement without results