pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Gareth Kirwan <gbjk@thermeoneurope.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Complex join query
Date: Wed, 29 May 2002 08:56:03 -0700 (PDT)
Message-ID: <20020529084849.K32750-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <001b01c20718$990f1ff0$55eaa8c0@gbjk1>


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





view thread (2+ messages)

Message-ID: <20020529084849.K32750-100000@megazone23.bigpanda.com>
Permalink:  ../20020529084849.K32750-100000@megazone23.bigpanda.com/
Also on:    postgresql.org/message-id/20020529084849.K32750-100000@megazone23.bigpanda.com

 · 

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 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-admin@postgresql.org
  Cc: sszabo@megazone23.bigpanda.com, gbjk@thermeoneurope.com
  Subject: Re: Complex join query
  In-Reply-To: <20020529084849.K32750-100000@megazone23.bigpanda.com>
  References: <001b01c20718$990f1ff0$55eaa8c0@gbjk1> <20020529084849.K32750-100000@megazone23.bigpanda.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