There are some similar questions around but they aren\'t quite what I\'m looking for, so forgive me if you think this is answered elsewhere.
I am basically looking
Constructing dynamic SQL or building your SQL queries with the aid of a programming language would look like this (in PHP for ex.):
$pdos = $pdo->query("SHOW TABLES LIKE '%_name'");
$tables = $pdos->fetchAll();
$query = 'SELECT * FROM '.implode(' UNION SELECT * FROM ');
$pdo->query($query);
The fetchAll
method will return an array containing the names of each table selected.
The implode($glue, $array)
function takes an array and concatenates every value in the array using the $glue
parameter - usually you take an array of values and implode them using $glue = ','
to create a coma separated list of values.
In our case the implode
has a partial query as $glue
in order to create one big UNION JOIN
query.
Once the final $query
is build it should look something like:
SELECT * FROM table_1_name
UNION
SELECT * FROM table_2_name
UNION
SELECT * FROM table_3_name
....
....
UNION
SELECT * FROM table_4000_name
The result should contain all of the DISTINCT
rows from all 4000 tables.
SELECT GROUP_CONCAT(
CONCAT('select * from ', table_name)
SEPARATOR ' union '
)
INTO @my_variable
FROM information_schema.tables
WHERE table_schema = 'dbname'
AND table_name LIKE '%_name';
PREPARE my_statement FROM @my_variable;
EXECUTE my_statement;
information_schema
database;CONCAT
function prefixes every table name with a a 'SELECT * FROM '
string;GROUP_CONCAT
does the job that implode
would have done in PHP;The INTO
clause makes sure the values are saved inside a variable named my_variable
;
The PREPARE
statement takes a string value (such as the one you saved in my_variable
) and checks if the value is an SQL query;
EXECUTE
statement takes a "prepared statement" and well... executes it.@my_variable is a temporary variable but it can only be of a scalar type (varchar, int, date, datetime, binary, float, double etc.) it is not an array.
The GROUP_CONCAT
function is an "aggregate function" which means it takes an aggregate value (similar concept to an array - in our case the result set of our query) and outputs a simple string result.