Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 42A0F7412C2 for ; Sat, 10 Dec 2011 19:02:13 -0400 (AST) Received: from lo.gmane.org ([80.91.229.12]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RZVvn-0001UW-4t for pgsql-sql@postgresql.org; Sat, 10 Dec 2011 23:02:14 +0000 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RZVvY-0004XQ-Lt for pgsql-sql@postgresql.org; Sun, 11 Dec 2011 00:01:56 +0100 Received: from ip-118-90-28-111.xdsl.xnet.co.nz ([118.90.28.111]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 11 Dec 2011 00:01:56 +0100 Received: from jasen by ip-118-90-28-111.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 11 Dec 2011 00:01:56 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: conditional FROM Date: 10 Dec 2011 22:32:50 GMT Organization: Dis (not Dat) Organisation Lines: 35 Message-ID: References: <20111210170332479879.3df30525@klingler.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-28-111.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Pg-Spam-Score: -6.1 (------) X-Archive-Number: 201112/45 X-Sequence-Number: 36106 On 2011-12-10, Richard Klingler 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