Select data from “show tables” MySQL query

后端 未结 13 1754
难免孤独
难免孤独 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条回答
  •  萌比男神i
    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.

提交回复
热议问题