[英]MySQL Dropping Tables
What syntax for MySQL would I use to drop multiple tables that have a similar pattern to them? 我将使用什么语法来删除多个具有类似模式的表? Something like:
就像是:
DROP TABLES FROM `Database1` LIKE "SubTable*"
Since DROP TABLE was supported by prepared statements, it can be done in this way - 由于DROP TABLE受到预准备语句的支持,因此可以通过这种方式完成 -
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables FROM information_schema.tables
WHERE table_schema = 'Database1' AND table_name LIKE 'SubTable%';
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt1 FROM @tables;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
正如在这个问题上所指出的,这里给出的答案(Angelin和Devart)在没有首先增加group_concat限制的情况下不会在所有情况下起作用,如下所示:
SET group_concat_max_len = 1024 * 1024 * 10;
No. But you can select tables names from information_schema
database: 不。但您可以从
information_schema
数据库中选择表名:
select table_name
from information_schema.tables
where table_schema = 'Database1'
and table_name like 'SubTable%'
And after that iterate the table names in result set and drop them 然后迭代结果集中的表名并删除它们
mysql> SELECT CONCAT( "DROP TABLE ", GROUP_CONCAT(TABLE_NAME) ) AS stmt
mysql> SELECT CONCAT(“DROP TABLE”,GROUP_CONCAT(TABLE_NAME))AS stmt
FROM information_schema.TABLES
来自information_schema.TABLES
WHERE TABLE_SCHEMA = "your_db_name" AND TABLE_NAME LIKE "ur condition" into outfile '/tmp/a.txt';
WHERE TABLE_SCHEMA =“your_db_name”AND TABLE_NAME将“ur condition”改为outfile'/tmp/a.txt';
mysql> source /tmp/a.txt;
mysql> source /tmp/a.txt;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.