pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jasen Betts <jasen@xnet.co.nz>
To: pgsql-sql@postgresql.org
Subject: Re: conditional FROM
Date: 10 Dec 2011 22:32:50 GMT
Message-ID: <jc0mmi$4nb$2@reversiblemaps.ath.cx> (raw)
References: <20111210170332479879.3df30525@klingler.net>

On 2011-12-10, Richard Klingler <richard@klingler.net> wrote:
> Good day...
>
> I'm trying to build a query for PGSQL 9.1 where a table has two 
> references with only one being used depending of the type of entry..
>
> For example, the table has following simplified structure:
>
> 	portid		primary key
> 	port2node	index to table node
> 	port2card	index to table card
>
> So how can I do a conditional FROM clause in the query depending on the 
> column port2node and port2card?
> If port2card is Null or 0 I don't want it in the FROM clause as the 
> query will return unneccessary duplicate
> row...the same goes for port2node being Null or 0...

use left outer join.

  SELECT * FROM 
     port
     LEFT OUTER JOIN node ON node.nodeid=port.port2node
     LEFT OUTER JOIN card ON card.cardid=port.port2card

or something like that.


You may find coalesce() useful to combine columns where node and card
both carry equivalent information.



-- 
⚂⚃ 100% natural




view thread (7+ messages)

Message-ID: <jc0mmi$4nb$2@reversiblemaps.ath.cx>
Permalink:  ../jc0mmi$4nb$2@reversiblemaps.ath.cx/
Also on:    postgresql.org/message-id/jc0mmi$4nb$2@reversiblemaps.ath.cx

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jasen@xnet.co.nz
  Subject: Re: conditional FROM
  In-Reply-To: <jc0mmi$4nb$2@reversiblemaps.ath.cx>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox