X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 4D8929DC882 for ; Thu, 5 Jan 2006 04:40:58 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 28334-02 for ; Thu, 5 Jan 2006 04:40:59 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id 2A7AF9DC8D9 for ; Thu, 5 Jan 2006 04:40:55 -0400 (AST) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id B8B8A418696; Thu, 5 Jan 2006 08:40:52 +0000 (GMT) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 9393C15EA5; Thu, 5 Jan 2006 08:40:56 +0000 (GMT) Message-ID: <43BCDB98.7030005@archonet.com> Date: Thu, 05 Jan 2006 08:40:56 +0000 From: Richard Huxton User-Agent: Thunderbird 1.5 (X11/20051025) MIME-Version: 1.0 To: "Scott, Casey" Cc: pgsql-sql@postgresql.org Subject: Re: JOIN question with multiple records References: <9A072150141F5C489DC92CFCB8C0FD4905E296D5@e2ksea2.wotc.wizards.com> In-Reply-To: <9A072150141F5C489DC92CFCB8C0FD4905E296D5@e2ksea2.wotc.wizards.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.046 required=5 tests=[AWL=0.046] X-Spam-Score: 0.046 X-Spam-Level: X-Archive-Number: 200601/23 X-Sequence-Number: 23812 Scott, Casey wrote: > I have 2 tables. One containing information about servers, and the other > containing information about IP addresses. > > E.G. > Server table: > > > name mac > mac2 > ------------------------------------------------------------- > SERVER1 00:0d:56:ba:ad:92 > SERVER2 00:0d:56:ba:ad:93 > 00:0d:56:ba:ad:96 > SERVER3 00:0d:56:ba:ad:94 > SERVER4 00:0d:56:ba:ad:95 > 00:0d:56:ba:ad:97 I think you've got the design of this table wrong. It looks like you're leaving mac2 NULL where the server has only one network-card. This is wrong - mac2 is not "unknown" it is "card not present" or similar (and the type of the column should then be not mac-address but mac-address-and-not-present). I'm also not sure how you will handle the case when a server has 3 network-cards. Also, if you want to know which server has a specific mac-addr then you'll need to check two columns with your current design. If possible I'd suggest reworking the table to something like: (name, card-id, mac-addr) and you'd then have: SERVER2 0 00:0d:56:ba:ad:93 SERVER2 1 00:0d:56:ba:ad:96 ... Then a crosstab function / case statement can reformat your query output as required. > SELECT servers.name,addresses.ipaddr,servers.application_mgr FROM > servers LEFT JOIN addresses ON addresses.mac = servers.mac OR > addresses.mac = servers.mac2 Well, if you can't change the structure of your tables you could do something like: SELECT ... FROM servers s LEFT JOIN addresses a1 ON s.mac = a1.mac LEFT JOIN addresses a2 ON s.mac = a2.mac The crucial bit is aliasing the "addresses" table twice. -- Richard Huxton Archonet Ltd