Query a database with results from multiple tables?

后端 未结 2 1983
無奈伤痛
無奈伤痛 2020-12-22 02:08

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

2条回答
  •  隐瞒了意图╮
    2020-12-22 02:34

    Generic example (in PHP):

    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.

    Specific example (in SQL-only format):

    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;
    
    • The first statement will get all of the table names from the information_schema database;
    • The CONCAT function prefixes every table name with a a 'SELECT * FROM ' string;
    • The 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;

    • The 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.

提交回复
热议问题