Re: join table with empty fields and default

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: quickcur(at)yahoo(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: join table with empty fields and default
Date: 2006-07-22 20:18:33
Message-ID: 20060722201833.GA72352@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Jul 22, 2006 at 12:47:42PM -0700, quickcur(at)yahoo(dot)com wrote:
> I would like to join table user and userwork, where if a user has a
> work, I list it. If he does not, I give it some default value "no work"

You could use an outer join and COALESCE.

http://www.postgresql.org/docs/8.1/interactive/tutorial-join.html
http://www.postgresql.org/docs/8.1/interactive/queries-table-expressions.html#QUERIES-JOIN
http://www.postgresql.org/docs/8.1/interactive/functions-conditional.html#AEN12639

Example:

SELECT u.id, u.name, u.address, COALESCE(uw.work, 'no work') AS work
FROM "user" AS u
LEFT OUTER JOIN userwork AS uw ON uw.userid = u.id;

--
Michael Fuhr

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message quickcur 2006-07-22 20:38:38 Re: join table with empty fields and default
Previous Message quickcur 2006-07-22 19:47:42 join table with empty fields and default