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

标签 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'

然后运行此查询的输出。

更新。

这是一个将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');

关于mysql - 将 mysql 查询应用于数据库中的每个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8398847/

相关文章:

mysql - 对于具有 MyISAM 存储引擎的高流量服务器,哪个查询在查询优化方面表现出色?

php - 检查数据库,如果数据不存在则插入

PHP 使用 mySQL-db 中的下拉值

php - 使用 Jquery Checkbox 发送多个表单

mysql - My-sql一对多关系连接获取左表列的总和

php - 使用多维数组更新 MySQL 数据库

mysql - 在 PRIMARY KEY 上使用 JOIN 时应该使用什么索引

php - 插入查询不起作用,即使它是正确的

php - 数据库中所有用户的减量字段

MYSQL存储过程: How to delete a just updated record