在 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 语句获取表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!