在MySQL中,我知道我可以在数据库中列出表:

SHOW TABLES

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

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

是否有一种方法可以使用标准的SELECT语句来获取表名,比如:

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

当前回答

Try:

select * from information_schema.tables

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

其他回答

插入、更新和删除操作如下:

$teste = array('LOW_PRIORITY', 'DELAYED', 'HIGH_PRIORITY', 'IGNORE', 'INTO', 'INSERT', 'UPDATE', 'DELETE', 'QUICK', 'FROM');
$teste1 = array("\t", "\n", "\r", "\0", "\x0B");
$strsql = trim(str_ireplace($teste1, ' ', str_ireplace($teste, '', $strsql)));
$nomeTabela = substr($strsql, 0, strpos($strsql, ' '));

print($nomeTabela);
exit;

我认为有必要指出,如果您想选择包含特定单词的表,可以使用select(而不是SHOW)轻松做到这一点。下面的查询可以轻松地将搜索范围缩小到包含“关键字”的表

SELECT *
FROM information_schema.tables
WHERE table_name like "%keyword%"

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

SELECT table_name FROM information_schema.tables;

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

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

现在,回答最初的问题,使用这个查询:

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

如果我们有多个数据库,并且我们需要为一个特定的数据库选择所有的表,我们可以使用TABLE_SCHEMA定义数据库名称为:

从information_schema中选择table_name。where TABLE_SCHEMA='dbname';

SELECT table_name 
FROM information_schema.tables 
WHERE table_schema = 'DATABASE'