Reputation: 54793
The table names in my mysql database are dynamically generated. Is there some way to select data from tables which have a name matching a pattern? I guess it will look like:
select * from 'table_id_%'
Upvotes: 19
Views: 17411
Reputation: 1178
You can use INFORMATION_SCHEMA TABLES table to find tables you want, here is documentation: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html . TABLES table has column NAME which represents names of tables. After finding table names you can run any sql queries you like.
Upvotes: 11
Reputation: 3364
That's not possible in the way you'd like to do it. However you could probably use prepared statements which are basically query-templates where you specificy the parameters (AFAIK also table names) that get replaced depending on your needs without copy and pasting the same query over and over again for different tables.
Upvotes: 2
Reputation: 321668
No, you can't do that with MySQL. Tables in a query can't be dynamically specified - you have to build the list in your application (or do several single-table queries).
Upvotes: 14