另一个oneliner用来提取表名称列表,mysql -sN …
然后在“ for…in…” shell循环中使用每个项目来删除它们:
for f in `mysql dbname -sN -e "SHOW TABLES LIKE 'bak\_%' "` ; do mysql dbname -rsN -e "DROP TABLE $f"; done
或(扩展版本)
for f in `mysql dbname -sN -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME LIKE 'bak\_%' "` ; do mysql dbname -rsN -e "DROP TABLE $f"; done
或使用“ group_concat”串联*表名(如果它们足够短):
tables=`mysql dbname -srN -e "SELECT GROUP_CONCAT(TABLE_NAME SEPARATOR ',') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME LIKE 'bak\_%' "`; mysql dbname -rsN -e "DROP TABLE $tables"
*某些限制,例如“ group_concat_max_len”的值(通常等于1024,请参见70个表)可能会造成干扰。
相同的原理,但用于转储除以“ bak_”开头的表以外的所有表:
for f in `mysql dbname -sN -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbname' AND NOT(TABLE_NAME LIKE 'bak\_%') "` ; do mysqldump -u [u] -p dbname "$f" >> dump_dbname.sql; done