using pg_tables and tablename in queries

From: solarsail <solarsail(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: using pg_tables and tablename in queries
Date: 2005-10-04 16:30:55
Message-ID: aa44f4a30510040930m510bbe8do4babd97b1b37c90d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I have a large number of tables with a common naming convention

mytable001, mytable002, mytable003 ... mytable00n

I would like to do a query across all of the tables, however I do not know
all of the tables before hand, and I do not want to ( cant ) manually
generate a query like

select * from mytable001, mytable002, mytable003

I have a query that returns the names of the tables I want to query:

select tablename from pg_tables where tablename like 'mytable%'

I have successfully done this query before. I remember it used a odd syntax;
I it was something like

select * from ( select tablename from pg_tables where tablename like
'mytable%' )

However the above query returns just a listing of tables, I need to use its
output in the FROM and treat the input as a table name.

( I've tried creating a Table Function that returns the above set and tried
to use that in a select clause, but I cant get it to work. )

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-10-04 17:01:31 Re: using pg_tables and tablename in queries
Previous Message Tom Lane 2005-10-04 14:50:07 Re: combination of function to simple query makes query slow