Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of wildcards in mysql table name

Tags:

mysql

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_%'
like image 730
Niyaz Avatar asked Oct 23 '09 10:10

Niyaz


People also ask

What is the use of wildcard in MySQL?

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.

What is the use of wildcards in a query?

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.

Does [] wildcard work in MySQL?

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.

What wildcards are available in MySQL and what do they represent?

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.


Video Answer


2 Answers

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).

like image 149
Greg Avatar answered Sep 21 '22 13:09

Greg


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.

like image 40
giolekva Avatar answered Sep 23 '22 13:09

giolekva