From: Andreas Joseph Krogh <andreak@officenet.no>
To: pgsql-sql@postgresql.org
Subject: Help with UNION query
Date: Sun, 4 Sep 2005 19:14:25 +0200
Message-ID: <200509041914.33245.andreak@officenet.no> (raw)
Hi all, I have the followin query:
SELECT g.id
from onp_group g
where g.groupname IN
(SELECT ug.groupname from onp_user_group ug WHERE ug.username = 'andreak')
UNION (SELECT child_id
FROM onp_group_children
WHERE group_id IN
(SELECT g.id from onp_group g
where g.groupname IN (SELECT ug.groupname from onp_user_group ug WHERE
ug.username = 'andreak')));
Any ideas on how I can simplyfy this query so that I only need to provide
ug.username='andreak' once? I would much rather prefer a query which doesn't
involve UNION or SUB-SELECTS.
Here are the table-definitions:
CREATE TABLE onp_group(
id integer PRIMARY KEY REFERENCES onp_entity(id) on delete cascade,
p_id integer REFERENCES onp_group(id) on delete cascade,
groupname varchar NOT NULL unique
);
CREATE TABLE onp_group_children(
group_id integer NOT NULL REFERENCES onp_group(id),
child_id integer NOT NULL REFERENCES onp_group(id),
UNIQUE(group_id, child_id)
);
CREATE TABLE onp_user_group(
groupname varchar not null REFERENCES onp_group(groupname) ON DELETE CASCADE
ON UPDATE CASCADE,
username varchar not null REFERENCES onp_user(username) ON DELETE CASCADE ON
UPDATE CASCADE,
primary key(groupname, username)
);
--
Andreas Joseph Krogh <andreak@officenet.no>
Senior Software Developer / Manager
gpg public_key: http://dev.officenet.no/~andreak/public_key.asc
------------------------+---------------------------------------------+
OfficeNet AS | The most difficult thing in the world is to |
Hoffsveien 17 | know how to do a thing and to watch |
PO. Box 425 Skøyen | somebody else doing it wrong, without |
0213 Oslo | comment. |
NORWAY | |
Phone : +47 22 13 01 00 | |
Direct: +47 22 13 10 03 | |
Mobile: +47 909 56 963 | |
------------------------+---------------------------------------------+
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: andreak@officenet.no
Subject: Re: Help with UNION query
In-Reply-To: <200509041914.33245.andreak@officenet.no>
* 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