I have a bunch of tables in a MySQL
database, some of them starting with phpbb_* which I wanted to delete all of them. Does someone know a way to do so instead of doing
drop table <tablename>;
Run Code Online (Sandbox Code Playgroud)
every single time? Like a regex?
drop table phpbb*
Run Code Online (Sandbox Code Playgroud)
or something like?
你可以使用这个MySQL程序:
DELIMITER $$
CREATE PROCEDURE drop_tables_like(pattern VARCHAR(255), db VARCHAR(255))
BEGIN
SELECT @str_sql:=CONCAT('drop table ', GROUP_CONCAT(table_name))
FROM information_schema.tables
WHERE table_schema=db AND table_name LIKE pattern;
PREPARE stmt from @str_sql;
EXECUTE stmt;
DROP prepare stmt;
END$$
DELIMITER ;
Run Code Online (Sandbox Code Playgroud)
要删除'test1'数据库中以'a'开头的所有表,您可以运行:
CALL drop_tables_like('a%', 'test1');
Run Code Online (Sandbox Code Playgroud)
参考:http://dev.mysql.com/doc/refman/5.5/en/drop-table.html