从“显示表格”中选择数据 MySQL查询
是否可以从 MySQL 中的 show table
中进行选择?
SELECT * FROM (SHOW TABLES) AS `my_tables`
沿着这些思路,尽管上面的方法不起作用(至少在 5.0.51a 上)。
Is it possible to select from show tables
in MySQL?
SELECT * FROM (SHOW TABLES) AS `my_tables`
Something along these lines, though the above does not work (on 5.0.51a, at least).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
你可能比你想象的更接近 - SHOW TABLES 已经表现出来了很像 SELECT 语句。 这是一个 PHP 示例,说明如何获取其“行”:
SHOW TABLES 的行为类似于单列表上的 SELECT。 该列名称是
Tables_in_
加上数据库名称。You may be closer than you think — SHOW TABLES already behaves a lot like a SELECT statement. Here's a PHP example of how you might fetch its "rows":
SHOW TABLES behaves like a SELECT on a one-column table. That column name is
Tables_in_
plus the database name.您不能像示例中那样将
SHOW
语句放入子查询中。 唯一可以进入子查询的语句是SELECT
。正如其他答案所述,您可以直接使用
SELECT
查询 INFORMATION_SCHEMA,并通过这种方式获得更大的灵活性。MySQL 的 SHOW 语句在内部只是针对 INFORMATION_SCHEMA 表的查询。
用户@physicalattraction 在大多数其他答案上发表了此评论:
相反,OP 的问题并没有说他们想要选择所有表中的数据。 他们说他们想从 SHOW TABLES 的结果中进行选择,这只是一个表名列表。
如果OP确实想从所有表中选择所有数据,那么答案是否定的,你不能通过一个查询来完成它。 每个查询必须显式命名其表。 您不能使表名成为变量或同一查询的另一部分的结果。 此外,给定查询结果的所有行必须具有相同的列。
因此,从所有表中选择所有数据的唯一方法是运行 SHOW TABLES,然后针对该结果中指定的每个表运行另一个查询。
You can't put
SHOW
statements inside a subquery like in your example. The only statement that can go in a subquery isSELECT
.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:
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.您是否考虑过查询 INFORMATION_SCHEMA.Tables? 如在
Have you looked into querying INFORMATION_SCHEMA.Tables? As in
这应该是一个好的开始。 有关更多信息,请查看INFORMATION_SCHEMA 表。
That should be a good start. For more, check INFORMATION_SCHEMA Tables.
我认为你想要的是 MySQL 的 information_schema 视图:
http://dev.mysql.com/doc/refman/ 5.0/en/tables-table.html
I think what you want is MySQL's information_schema view(s):
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
您可以创建一个存储过程并将表名称放入游标中,然后循环遍历表名称以显示数据。
存储过程入门:
http://www.mysqltutorial.org/getting-started- with-mysql-stored-procedures.aspx
创建游标:
http://www.mysqltutorial.org/mysql-cursor/
例如,
然后调用存储过程:
You can create a stored procedure and put the table names in a cursor, then loop through your table names to show the data.
Getting started with stored procedure:
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx
Creating a cursor:
http://www.mysqltutorial.org/mysql-cursor/
For example,
Then call the stored procedure:
在MySql 5.1中你可以尝试
输出:
in MySql 5.1 you can try
output:
是的,SELECT from table_schema 对于系统管理非常有用。 如果您有很多服务器、数据库、表...有时您需要删除或更新一堆元素。 例如,要创建 DROP 所有前缀名称为“wp_old_...”的表的查询:
Yes, SELECT from table_schema could be very usefull for system administration. If you have lot of servers, databases, tables... sometimes you need to DROP or UPDATE bunch of elements. For example to create query for DROP all tables with prefix name "wp_old_...":
这将返回以下评论:myTable.myColumnName
This will return the comment on: myTable.myColumnName
我不明白您为什么要使用 SELECT * FROM 作为语句的一部分。
12.5.5.30。 显示表语法
I don't understand why you want to use
SELECT * FROM
as part of the statement.12.5.5.30. SHOW TABLES Syntax
我想你想要
SELECT * FROM INFORMATION_SCHEMA.TABLES
参见http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
I think you want
SELECT * FROM INFORMATION_SCHEMA.TABLES
See http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
据我所知,除非您从
INFORMATION_SCHEMA
中进行选择,正如其他人提到的那样。然而,SHOW 命令非常灵活,
例如:
Not that I know of, unless you select from
INFORMATION_SCHEMA
, as others have mentioned.However, the
SHOW
command is pretty flexible,E.g.:
计数:
列出:
To count:
To list: