determining which table to lookup depending on data values

From: Steve Castellotti <SteveC(at)innocent(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: determining which table to lookup depending on data values
Date: 2006-12-17 07:14:43
Message-ID: 1166339683.3242.25.camel@iliad
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello all-

I'm working with a poorly-designed schema and need to do a lookup in
one table who's name I have to pull from a second table. I'm wondering
if its possible to do something like this in PostgreSQL:

Say I have three tables:

CREATE TABLE audio (id int4, name varchar(32));
CREATE TABLE video (id int4, name varchar(32));
CREATE TABLE media (id int4, table_name varchar(32), table_id int4);

with data:

INSERT INTO audio VALUES (0, 'file.wav');
INSERT INTO video VALUES (0, 'file.avi');
INSERT INTO media VALUES (0, 'audio', 0);
INSERT INTO media VALUES (1, 'video', 0);

Is there any way, especially in a single statement, where I can get
the name of a few if I only have the media table's id?

SELECT name FROM (SELECT table_name FROM media WHERE media_id=1);

Of course I get back an error about needing to give my subquery an
alias, but even if I do so I only get the output from the subquery,
instead of being able to tell PostgreSQL to use that output as the name
of the table it should use to look up "name"

The original design was meant for a multimedia play which could use
a mixture of audio and video in a playlist, and that which housed all of
the entries for the playlist could just refer to an id for the media
table. When the software plays it has to pull this information out and
do a single lookup for each item. This looses referential integrity and
means you can't get from a play down to a specific file with one query.

Thanks in advance!

Steve Castellotti

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Yonatan Ben-Nes 2006-12-17 09:36:04 PDOStatement:closeCursor
Previous Message Angva 2006-12-17 05:48:03 Re: out of memory woes