zoukankan      html  css  js  c++  java
  • MYSQL 存储 while 统计每个表

    群里一朋友,有一需求就是获取数据库每个表的总计(条数)
    思路:动态传入表名, count(1)

    --
    1.执行这句。获取所有表名 Create table temp_tb (select t.table_name,@rownum:=@rownum+1 as num from information_schema.tables t,(select @rownum:=0 ) b where t.table_schema='test' and table_name not in ('temp','temp_tb')); -- 2.同理获取表结构,把要统计的结果跟对应的表名放在这个表里面 Create table temp (select t.table_name,@rownum:=@rownum+1 as num from information_schema.tables t,(select @rownum:=0 ) b where t.table_schema='test' and table_name not in ('temp','temp_tb')); -- 3.删除表数据保留表结构 delete from temp; -- 4.创建存储
    -- 4.创建存储
    create PROCEDURE WhileLoopProc()
    BEGIN
    select @num :=1,@len :=count(1) from temp_tb;
    while @num<@len do
    select @name :=table_name from temp_tb where num =@num;
    set @rownum := concat('select count(1)',' as ',@name ,' into  @temp from ', @name);   
    set @num:=@num+1;
    prepare stmt from @rownum;   
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt ;
    insert into temp(table_name,num) values(@name,@temp); -- 把执行出来的结果保存到结果表中
     end while;
    end;
    -- 5.执行存储 
    call WhileLoopProc;
    -- 6.查询结果
    select * from temp;
    完事!


  • 相关阅读:
    Mac 下安装Ant
    MAMP 10.10下启动报错解决方案
    [转]常用iOS图片处理方法
    Mac下Android SDK更新不了的解决办法
    细说23+1种设计模式
    mysql应该了解的知识点
    java快排思想
    简介一下 i++和++i&&i=i+i,i+=1;的区别
    对int类型的数据,如何让获取长度
    第一次写博客
  • 原文地址:https://www.cnblogs.com/1-Admin/p/9068235.html
Copyright © 2011-2022 走看看