E
E
Elvis2020-11-04 15:08:08
PostgreSQL
Elvis, 2020-11-04 15:08:08

How to make a selection from tables with the same structure?

Hey!
There are several tables with the same structure but different names.
it is necessary to make a selection from all tables.
Everything is simple if the names of the tables are known, you can use union, but the tables appear dynamically.
You can select the table names that I need:

SELECT table_name FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME LIKE 'need_table_%'

Here from this list it is necessary to make association of tables. All required tables start with "need_table_"
Can this be done with just sql? Or resort to, for example, python? it would be desirable of course on pure sql.
using Postgres 9.5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-11-04
@trapwalker

So-so idea to dynamically form tables, and then get decent hemorrhoids to deal with these tables.
What you need is written here in the documentation.
But what prevented you from placing all the data in one table, and what you use as an addition to the name of your individual tables is simply stored in an indexed column?
It's just that if you can't find a solution to such a simple question, chances are that you made a mistake earlier when choosing such a controversial architectural decision to dynamically create the same type of tables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question