Re: If table A value IS NULL then table B

From: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
To: Martín Marqués <martin(at)bugs(dot)unl(dot)edu(dot)ar>
Cc: Marco Lazzeri <marcomail(at)noze(dot)it>, pgsql-general(at)postgresql(dot)org
Subject: Re: If table A value IS NULL then table B
Date: 2004-01-23 20:28:42
Message-ID: 20040123202842.GC30740@dcc.uchile.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jan 23, 2004 at 05:15:56PM -0300, Martín Marqués wrote:
> Mensaje citado por Marco Lazzeri <marcomail(at)noze(dot)it>:
>
> > I want to write a query on table 'main' that if 'id_other_table' is null
> > returns value from itself, from table 'other' otherwise.
>
> SELECT CASE WHEN id_other_table IS NULL THEN id_other_table
> ELSE id
> FROM main,other

What about COALESCE?

SELECT COALESCE(id_other_table, id) FROM main, other WHERE ...

(Also probably an OUTER JOIN is needed -- see
http://www.varlena.com/GeneralBits/56.php)

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"Saca el libro que tu religión considere como el indicado para encontrar la
oración que traiga paz a tu alma. Luego rebootea el computador
y ve si funciona" (Carlos Duclós)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martín Marqués 2004-01-23 20:33:34 Re: sequence in schema -- broken default
Previous Message Joshua D. Drake 2004-01-23 20:25:05 Re: sequence in schema -- broken default