MySQL 中删除特定前缀的表

删除某个表我知道是使用

DROP TABLE

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

阅读 9.1k
2 个回答

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

  • 写一个UDF(用户自定义函数)来实现,先查某DB下面以wp_开头的表,再删除之
  • 用bash shell,类似这样(语法包含错误,只是示意思路,请自行调试):
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

只能拼接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...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题