Re: Setting a default value for a select statement without results

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Setting a default value for a select statement without results
Date: 2013-02-06 04:07:37
Message-ID: kesku9$ih0$2@gonzo.reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 2013-02-06, JORGE MALDONADO <jorgemal1960(at)gmail(dot)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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Dev Kumkar 2013-02-06 06:57:28 Re: Querying multiple database
Previous Message Wayne Cuddy 2013-02-06 01:13:49 index scan vs bitmap index scan