微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将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 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');

原文地址:https://www.jb51.cc/mysql/433625.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐