My database has an unknown amount of tables, named with the same prefix followed by a random string: _ct_<random_string>
.
Each of these tables has the same data structure.
I also have another table in which I store the name of each table for future reference.
The following table name is 'content_types' and provides reference for other tables:
+-----------------+
| id | table_name |
+-----------------+
| 1 | _ct_table1 |
| 2 | _ct_table2 |
| 3 | _ct_table3 |
| 4 | _ct_fdfsf |
| 5 | _ct_535dgf |
...
+-----------------+
The following are example to the referenced tables in 'content_types'.'table_name':
_ct_table1: _ct_table2:
+-----------------------------------+ +-----------------------------------+
| id | title | subtitle | date | | id | title | subtitle | date |
+-----------------------------------+ +-----------------------------------+
| 1 | fddfs | ... | 2012-08-22| | 4 | fddfs | ... | 2012-03-18|
| 2 | bdsf | | 2012-08-22| | 5 | b4sf | dfs | 2012-07-20|
| 3 | sfdsd | | 2012-08-22| | 6 | sfdsd | | 2012-03-12|
+-----------------------------------+ +-----------------------------------+
Is it possible to create a query with a dynamic UNION
statement based on the tables listed in 'content_types'.'table_name'?
Something like this:
select id,title,subtitle,date
from
(
select table_name
from content_types
);