Using table name in column for joining

From: Antonio Goméz Soto <antonio(dot)gomez(dot)soto(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Using table name in column for joining
Date: 2010-01-05 15:30:25
Message-ID: 4B435B11.2030909@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I have a column in a table that contains the name of another table,
and the id in that table.

I would like to use this in a join statement. Is that possible?

example:

create table animal (
id serial,
table_name varchar(8) default 'monkey' CHECK (table_name IN ('monkey', 'elephant')),
table_id integer
);

create table monkey (
id serial,
name varchar(20)
);

create table elephant (
id serial,
name varchar(20)
);

and I want to do:

select animal.name from animal left join animal.table_name on animaltablename.id = animal.table_id;

or something like that.

I know it's probably considered bad SQL, but what else can I do?

Thanks,
Antonio

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2010-01-05 15:34:48 Re: zic error to install 8.4.0
Previous Message Adrian Klaver 2010-01-05 15:27:45 Re: FM format modifier does not remove leading zero from year