How to make a query from a mysql meta query?

1

Suppose I want to get all the tables that have a column that is named 'name' . This can be easily achieved by using the 'information_schema' scheme provided by mysql . However, suppose I want to list all the names in the 'name' column of all the tables that have a column that is named like this.

How can I do this?

    
asked by user5705 21.04.2016 в 22:42
source

1 answer

1

Try a prepared statement, 14.5 SQL Syntax for Prepared Statements , like the following:

SELECT
  GROUP_CONCAT(
    CONCAT(
      'SELECT 'nombre'
       FROM ', 'TABLE_SCHEMA', '.', 'TABLE_NAME'
    ) SEPARATOR ' UNION ALL '
  ) INTO @'qry'
FROM
  'information_schema'.'COLUMNS'
WHERE 'COLUMN_NAME' = 'nombre';

PREPARE 'stmt' FROM @'qry';
EXECUTE 'stmt';
DEALLOCATE PREPARE 'stmt';

Take care of the server system variable group_concat_max_len , to avoid truncating the result.

    
answered by 23.04.2016 в 15:53