我们可以在 show 语句中使用像 concat() group_concat() 这样的函数吗?

Can we use functions like concat() group_concat() in show statement?

比如我运行命令

mysql> 显示 tables;

数据库中的表

一个

b

c

d

我想使用它,例如 concat('drop table ', tablename, ';');

这样我的结果集就会变成

下降table一个;

下降 table b;

下降 table c;

等等....

使用21.24 The INFORMATION_SCHEMA TABLES Table.

尝试:

SELECT
  CONCAT('drop table ', `TABLE_NAME`, ';')
FROM
  `INFORMATION_SCHEMA`.`TABLES`
WHERE
  `TABLE_SCHEMA` = DATABASE() AND
  `TABLE_TYPE` = 'BASE TABLE';

SQL Fiddle demo

只是想补充一点,如果您想生成数据库列表,请使用类似的东西:

SELECT CONCAT('DROP DATABASE `', `table_schema`, '`;') FROM (
    SELECT `table_schema`
    FROM `INFORMATION_SCHEMA`.tables
    WHERE `table_schema` != "INFORMATION_SCHEMA" && `table_schema` != "performance_schema"
    GROUP BY `table_schema`
) as dbs

此外,如果您希望将所有删除数据库查询放在一行中,请使用 GROUP_CONCAT

SELECT GROUP_CONCAT(ts SEPARATOR ' ') as drop_databases_query FROM (SELECT CONCAT('DROP DATABASE `', `table_schema`, '`;') as ts FROM (
    SELECT `table_schema`
    FROM `INFORMATION_SCHEMA`.tables
    WHERE `table_schema` != "INFORMATION_SCHEMA" && `table_schema` != "performance_schema"
    GROUP BY `table_schema`
) as dbs) as concatted

它会产生类似于

的结果
DROP DATABASE `mydb`; DROP DATABASE `mydb2`; DROP DATABASE `mysql`; DROP DATABASE `sys`;