zoukankan      html  css  js  c++  java
  • MySQL:按前缀批量删除表格

    想要实现mysql>drop table like "prefix_%"

    没有直接可用的命令,不过可以通过mysql语法来组装,

    SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) 
    AS statement FROM information_schema.tables 
    WHERE table_schema = 'database_name' AND table_name LIKE 'myprefix_%';

    然后执行该组装后的命令,比如从slowquery表中删除掉所有tmp_开头的表:

    SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) AS statement FROM information_schema.tables WHERE table_schema = 'slowquery' AND table_name LIKE 'tmp_%';

    输出结果:

    drop table tmp_201301,tmp_201302,tmp_201351,tmp_201352; 


    执行之即可。

    我自己现实中的例子:

    /*
    自动生成删除动态表SQL语句
    用法:拷贝到查询分析器中执行,把结果拷贝出来再执行就OK了。
    */
    SELECT REPLACE(GROUP_CONCAT(a.dropSql),',','') AS dropSql
    FROM
    (
    SELECT CONCAT( 'drop table ', table_name, ';' ) AS dropSql
    FROM information_schema.tables 
    WHERE table_name LIKE 'slavetablemgr%'
    
    UNION
    
    SELECT CONCAT( 'drop table ', table_name, ';' ) AS dropSql
    FROM information_schema.tables 
    WHERE table_name LIKE 'DevicePlugin_%'
    
    UNION
    
    SELECT CONCAT( 'drop table ', table_name, ';' )  AS dropSql
    FROM information_schema.tables 
    WHERE table_name LIKE 'historyrecordhour%'
    
    UNION
    
    SELECT CONCAT( 'drop table ', table_name, ';' )  AS dropSql
    FROM information_schema.tables 
    WHERE table_name LIKE 'historyrecordminute%'
    
    UNION
    
    SELECT CONCAT( 'drop table ', table_name, ';' )  AS dropSql
    FROM information_schema.tables 
    WHERE table_name LIKE 'historyrecordsecond%'
    ) a;
  • 相关阅读:
    NOIP2018 模拟赛(二十二)雅礼NOI
    浅谈左偏树在OI中的应用
    HDU3062&&HDU1814
    2-SAT超入门讲解
    bitset常用用法&&简单题分析
    NOIp2014提高组初赛错题简析
    2018十月刷题列表
    BZOJ 4804: 欧拉心算
    Luogu P2568 GCD
    Luogu P4137 Rmq Problem / mex
  • 原文地址:https://www.cnblogs.com/zjoch/p/4431221.html
Copyright © 2011-2022 走看看