Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

possible to create sql query with table wildcards?

Tags:

sql

This is probably a simple question, but i have been unable to find a solution online, any help would be much appreciated.

I'm trying to create an SQL query in PHP and would like to somehow apply a wild card to the TABLE filter... something perhaps like.... select * from %_table_%. However, I have only so far been able to see filters for column values not table names.

as an example i would have tables such as:

jan_table_1
feb_table_1
jan_table_2
feb_table_2

and would want to say, select only tables with a "jan" prefix... or "1" suffix.

Is there a quick and easy solution to this that I have not seen? Thanks in advance!

like image 507
Rees Avatar asked Mar 09 '10 10:03

Rees


People also ask

How do you do a wildcard in SQL query?

To broaden the selections of a structured query language (SQL-SELECT) statement, two wildcard characters, the percent sign (%) and the underscore (_), can be used. The percent sign is analogous to the asterisk (*) wildcard character used with MS-DOS.

Do wildcards work in in SQL?

SQL WildcardsA 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 wildcard table in SQL?

A wildcard table enables you to query multiple tables using concise SQL statements. A wildcard table represents a union of all the tables that match the wildcard expression. Wildcard tables are available only in Google Standard SQL. For equivalent functionality in legacy SQL, see Table wildcard functions.

Which 2 wildcards are used in SQL?

SQL supports two wildcard operators in conjunction with the LIKE operator which are explained in detail in the following table. Sr.No. Matches one or more characters. Note − MS Access uses the asterisk (*) wildcard character instead of the percent sign (%) wildcard character.


2 Answers

In Sql server you can query for the table names you want like this

select * from sys.tables where name like '%table%'

In your code you could loop through the table names and execute your query on each table and merge the results. Most other RDBMS have similar functionality.

like image 144
Mike Two Avatar answered Sep 22 '22 00:09

Mike Two


There isn't. But the tables shouldn't be separated by month. Instead appropriate indexes should be used to speed up access.

like image 20
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 00:09

Ignacio Vazquez-Abrams