在 MySQL 中使用 SELECT 语句获取表名 [英] Get table names using SELECT statement in MySQL

查看:85
本文介绍了在 MySQL 中使用 SELECT 语句获取表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MySQL 中,我知道我可以使用以下命令列出数据库中的表:

In MySQL, I know I can list the tables in a database with:

SHOW TABLES

但是,我想将这些表名插入到另一个表中,例如:

However, I want to insert these table names into another table, for instance:

INSERT INTO metadata(table_name) SHOW TABLES /* does not work */

有没有办法使用标准的 SELECT 语句获取表名,例如:

Is there a way to get the table names using a standard SELECT statement, something like:

INSERT INTO metadata(table_name) SELECT name FROM table_names /* what should table_names be? */

推荐答案

要获取所有表的名称,请使用:

To get the name of all tables use:

SELECT table_name FROM information_schema.tables;

要从特定数据库中获取表的名称,请使用:

To get the name of the tables from a specific database use:

SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_database_name';

现在,要回答原始问题,请使用以下查询:

Now, to answer the original question, use this query:

INSERT INTO table_name
    SELECT table_name FROM information_schema.tables
        WHERE table_schema = 'your_database_name';

更多详情参见:http://dev.mysql.com/doc/refman/5.0/en/information-schema.html

这篇关于在 MySQL 中使用 SELECT 语句获取表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆