加入收藏 | 设为首页 | 会员中心 | 我要投稿 商洛站长网 (https://www.0914zz.com/)- AI应用、CDN、边缘计算、云计算、物联网!
当前位置: 首页 > 数据库 > MySql > 正文

将mysql查询应用于数据库中的每个表

发布时间:2021-01-17 17:43:16 所属栏目:MySql 来源:互联网
导读:有没有办法将查询应用于mysql数据库中的每个表?就像是SELECT count(*) FROM {ALL TABLES} -- gives the number of count(*) in each Table 和DELETE FROM {ALL TABLES} -- Like DELETE FROM TABLE applied on each Tabl

有没有办法将查询应用于mysql数据库中的每个表?

就像是

SELECT count(*) FROM {ALL TABLES}
-- gives the number of count(*) in each Table

DELETE FROM {ALL TABLES}
-- Like DELETE FROM TABLE applied on each Table
最佳答案
select sum(table_rows) as total_rows
from information_schema.tables
where table_schema = 'your_db_name'

要注意这只是一个近似值

要删除所有表格的内容,您可以执行以下操作

select concat('truncate ',table_name,';')
from information_schema.tables
where table_schema = 'your_db_name'

然后运行此查询的输出.

UPDATE.

这是将truncate table应用于特定数据库中的所有表的存储过程

delimiter //
drop procedure if exists delete_contents //
create procedure delete_contents (in db_name varchar(100))
begin
declare finish int default 0;
declare tab varchar(100);
declare cur_tables cursor for select table_name from information_schema.tables where table_schema = db_name and table_type = 'base table';
declare continue handler for not found set finish = 1;
open cur_tables;
my_loop:loop
fetch cur_tables into tab;
if finish = 1 then
leave my_loop;
end if;

set @str = concat('truncate ',tab);
prepare stmt from @str;
execute stmt;
deallocate prepare stmt;
end loop;
close cur_tables;
end; //
delimiter ;

call delete_contents('your_db_name');

(编辑:商洛站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读