Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 87E06475E9D for ; Wed, 29 May 2002 11:57:52 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 2DC05475C21 for ; Wed, 29 May 2002 11:55:59 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id E6B10D5FA; Wed, 29 May 2002 08:56:03 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id DC76C5C01; Wed, 29 May 2002 08:56:03 -0700 (PDT) Date: Wed, 29 May 2002 08:56:03 -0700 (PDT) From: Stephan Szabo To: Gareth Kirwan Cc: Subject: Re: Complex join query In-Reply-To: <001b01c20718$990f1ff0$55eaa8c0@gbjk1> Message-ID: <20020529084849.K32750-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200205/250 X-Sequence-Number: 4425 On Wed, 29 May 2002, Gareth Kirwan wrote: > Hello all, > Let's dive straight in, eh? > > 1. > Table: current_sessions > Fields: Server (int Foreign Key servers.id) > Client (int Foreign Key clients.id) > status (vc(10)) > > 2. > Table: clients > Fields: id (serial Pkey) > username (UNIQUE) > > 3. > Table: servers > Fields: id (serial Pkey) > username (UNIQUE) > > Normally, the username of the client should come from the clients table, > hence you'd inner join plainly. > IF the status is 'ADMIN' however - it means both the server and the client > come from the servers table. If you're actually using foreign keys for Client, you may have some other problems with that structure since it seems like you've got a key pointing off into the wrong table when status is ADMIN. > HOW ( if at all ) can I pull this off in a join without having to > restructure the tables? I'd suggest using UNION ALL. Get all the non admin ones with a join on client UNION ALL Get all the admin ones with another join on server