From: | Edmund <ebacon-xlii(at)onesystem(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: joining tables |
Date: | 2006-01-23 16:57:38 |
Message-ID: | m3lkx6rif1.fsf@elb_lx.onesystem.ca |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
oayasli(at)gmail(dot)com writes:
> Hi,
>
> If you have two tables, each with a column called "keys" and a column
> called "values", and they are both incomplete, such as:
>
> table 1:
>
> keys | values
> -----+----------
> 1 | (null)
> 2 | two
> 3 | (null)
>
> table 2:
>
> keys | values
> -----+---------
> 1 | one
> 2 | (null)
> 3 | three
>
> is there a way to join them, in order to get:
>
> keys | values
> -----+---------
> 1 | one
> 2 | two
> 3 | three
>
> The closest I could get was with NATURAL FULL JOIN:
>
> keys | values
> -----+---------
> 1 | one
> 1 | (null)
> 2 | two
> 2 | (null)
> 3 | three
> 3 | (null)
>
> Thanks
Try something like:
SELECT key,
CASE when table1.value IS NOT NULL THEN k1.value
ELSE table2.value END as value
FROM table1
FULL JOIN table2 USING(key);
You might want to use 'IS DISTINCT FROM table2.value' if you want the
value for table1 to be returned in preference to table2.value.
From | Date | Subject | |
---|---|---|---|
Next Message | Janning Vygen | 2006-01-23 17:33:50 | Re: invalid memory alloc request size |
Previous Message | Lincoln Yeoh | 2006-01-23 16:52:45 | Re: RAID 5 and postgresql |