Select data from “show tables” MySQL query

后端 未结 13 1670
难免孤独
难免孤独 2020-12-01 04:15

Is it possible to select from show tables in MySQL?

SELECT * FROM (SHOW TABLES) AS `my_tables`

Something along these lines, th

相关标签:
13条回答
  • 2020-12-01 04:47

    I think you want SELECT * FROM INFORMATION_SCHEMA.TABLES

    See http://dev.mysql.com/doc/refman/5.0/en/tables-table.html

    0 讨论(0)
  • 2020-12-01 04:47

    You may be closer than you think — SHOW TABLES already behaves a lot like SELECT:

    $pdo = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
    foreach ($pdo->query("SHOW TABLES") as $row) {
        print "Table $row[Tables_in_$dbname]\n";
    }
    
    0 讨论(0)
  • 2020-12-01 04:49

    You can't put SHOW statements inside a subquery like in your example. The only statement that can go in a subquery is SELECT.

    As other answers have stated, you can query the INFORMATION_SCHEMA directly with SELECT and get a lot more flexibility that way.

    MySQL's SHOW statements are internally just queries against the INFORMATION_SCHEMA tables.

    User @physicalattraction has posted this comment on most other answers:

    This gives you (meta)information about the tables, not the contents of the table, as the OP intended. – physicalattraction

    On the contrary, the OP's question does not say that they want to select the data in all the tables. They say they want to select from the result of SHOW TABLES, which is just a list of table names.

    If the OP does want to select all data from all tables, then the answer is no, you can't do it with one query. Each query must name its tables explicitly. You can't make a table name be a variable or the result of another part of the same query. Also, all rows of a given query result must have the same columns.

    So the only way to select all data from all tables would be to run SHOW TABLES and then for each table named in that result, run another query.

    0 讨论(0)
  • 2020-12-01 04:50

    I think what you want is MySQL's information_schema view(s): http://dev.mysql.com/doc/refman/5.0/en/tables-table.html

    0 讨论(0)
  • 2020-12-01 04:58

    Have you looked into querying INFORMATION_SCHEMA.Tables? As in

    SELECT ic.Table_Name,
        ic.Column_Name,
        ic.data_Type,
        IFNULL(Character_Maximum_Length,'') AS `Max`,
        ic.Numeric_precision as `Precision`,
        ic.numeric_scale as Scale,
        ic.Character_Maximum_Length as VarCharSize,
        ic.is_nullable as Nulls, 
        ic.ordinal_position as OrdinalPos, 
        ic.column_default as ColDefault, 
        ku.ordinal_position as PK,
        kcu.constraint_name,
        kcu.ordinal_position,
        tc.constraint_type
    FROM INFORMATION_SCHEMA.COLUMNS ic
        left outer join INFORMATION_SCHEMA.key_column_usage ku
            on ku.table_name = ic.table_name
            and ku.column_name = ic.column_name
        left outer join information_schema.key_column_usage kcu
            on kcu.column_name = ic.column_name
            and kcu.table_name = ic.table_name
        left outer join information_schema.table_constraints tc
            on kcu.constraint_name = tc.constraint_name
    order by ic.table_name, ic.ordinal_position;
    
    0 讨论(0)
  • 2020-12-01 04:58

    I don't understand why you want to use SELECT * FROM as part of the statement.

    12.5.5.30. SHOW TABLES Syntax

    0 讨论(0)
提交回复
热议问题