From: Scott, Casey <Casey.Scott@wizards.com>
To: pgsql-sql@postgresql.org
Subject: JOIN question with multiple records
Date: Wed, 4 Jan 2006 11:54:50 -0800
Message-ID: <9A072150141F5C489DC92CFCB8C0FD4905E296D5@e2ksea2.wotc.wizards.com> (raw)
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
Address table:
ip mac
--------------------------------
10.0.0.1 00:0d:56:ba:ad:92
10.0.0.2 00:0d:56:ba:ad:92
10.0.0.3 00:0d:56:ba:ad:94
10.0.0.4 00:0d:56:ba:ad:95
I need a query that will return all the IP addresses that match a
server's mac address along with the rest of the information about the
server in this format:
name ip mac
mac2
-----------------------------------------------------------------------
SERVER1 10.0.0.1 00:0d:56:ba:ad:92
10.0.0.2
SERVER2 00:0d:56:ba:ad:93
00:0d:56:ba:ad:96
SERVER3 10.0.0.3 00:0d:56:ba:ad:94
SERVER4 10.0.0.4 00:0d:56:ba:ad:95
However, the best that I've done is to return a record of the server for
every IP that matches in the address table:
name ip mac
mac2
-----------------------------------------------------------------------
SERVER1 10.0.0.1 00:0d:56:ba:ad:92
SERVER1 10.0.0.2 00:0d:56:ba:ad:92
SERVER2 00:0d:56:ba:ad:93
00:0d:56:ba:ad:96
SERVER3 10.0.0.3 00:0d:56:ba:ad:94
SERVER4 10.0.0.4 00:0d:56:ba:ad:95
An abbreviate version of query looks like this. Ultimately there will be
a WHERE condition on the query:
SELECT servers.name,addresses.ipaddr,servers.application_mgr FROM
servers LEFT JOIN addresses ON addresses.mac = servers.mac OR
addresses.mac = servers.mac2
Does anyone have any suggestions on this?
Regards,
Casey
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: Casey.Scott@wizards.com
Subject: Re: JOIN question with multiple records
In-Reply-To: <9A072150141F5C489DC92CFCB8C0FD4905E296D5@e2ksea2.wotc.wizards.com>
References: <9A072150141F5C489DC92CFCB8C0FD4905E296D5@e2ksea2.wotc.wizards.com>
* 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