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_%'
MySQL Wildcards A wildcard character is used to substitute one or more characters in a string. Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
To locate a specific item when you can't remember exactly how it is spelled, try using a wildcard character in a query. Wildcards are special characters that can stand in for unknown characters in a text value and are handy for locating multiple items with similar, but not identical data.
Wildcards used in all types of databases like MySQL, MS Access, Oracle. Wildcard work the same as regular expressions works. Multiple wildcards can be used at once while searching, filtering in database. All wildcards can be used in the SQL query either individually or in a combination of any other wildcards.
MySQL provides two wildcard characters for constructing patterns: percentage % and underscore _ . The percentage ( % ) wildcard matches any string of zero or more characters. The underscore ( _ ) wildcard matches any single character.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With