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!
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.
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.
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.
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.
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.
There isn't. But the tables shouldn't be separated by month. Instead appropriate indexes should be used to speed up access.
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