首页 > MySQL 中删除特定前缀的表

MySQL 中删除特定前缀的表

删除某个表我知道是使用

DROP TABLE

这种命令,如果我只想删除以WP_开头的表呢该怎么写SQL呢?


只能拼接SQL语句然后动态执行了。。。

set @str = (select concat('drop table ', group_concat(table_name separator ','),';')
from information_schema.tables
where table_schema = 'your_schema' and table_name like 'WP__%');
prepare stmt from @str;
execute stmt;
deallocate prepare stmt;

可以参考下 http://stackoverflow.com/questions/54...


mysql的drop table不支持通配符,所以,你的需求没办法用一条SQL语句搞定,你有两个选择:

for table_name in `mysql -uroot -e 'use your_db; show tables' | grep wp_`
do
  mysql -uroot -e 'use your_db; drop table $table_name if exists'
done
【热门文章】
【热门文章】