select sum(cnt) from
(
select count(*) as cnt from table1
union ALL
select count(*) as cnt from table2
union ALL
select count(*) as cnt from table3
)t1
select concat("select '",table_name,"', count(*) from ",table_name,";")
from `information_schema`.`tables`
WHERE `table_schema` = '[your schema here]';
SET @tableSchema = 'my_schema';
SET SESSION group_concat_max_len = 10000000;
SET @rowCounts = (
SELECT group_concat(CONCAT('SELECT ''',TABLE_NAME,''', COUNT(*) FROM ', TABLE_NAME) SEPARATOR ' union all ')
FROM information_schema.tables WHERE table_schema = @tableSchema
);
PREPARE statement FROM @rowCounts;
EXECUTE statement;
-- don't run dealloc until you've exported your results ;)
DEALLOCATE PREPARE statement;
DROP PROCEDURE IF EXISTS `database_tables_row_count`;
DELIMITER $$
CREATE PROCEDURE `database_tables_row_count`(IN tableSchema VARCHAR(255))
BEGIN
DECLARE msg VARCHAR(128);
IF (SELECT COUNT(TABLE_NAME) FROM information_schema.tables WHERE table_schema = `tableSchema`) = 0 THEN
SET msg = CONCAT('Unknown database \'', `tableSchema`, '\'');
SIGNAL SQLSTATE '42000' SET MESSAGE_TEXT = msg, MYSQL_ERRNO = 1049;
END IF;
SET SESSION group_concat_max_len = 10000000;
SET @rowCounts = (
SELECT group_concat(CONCAT('SELECT ''',TABLE_NAME,''' AS `table`, COUNT(*) AS `row_count` FROM ', `tableSchema`, '.', TABLE_NAME) SEPARATOR ' union all ')
FROM information_schema.tables WHERE table_schema = `tableSchema`
AND TABLE_TYPE = 'BASE TABLE'
);
IF @rowCounts IS NOT NULL THEN
PREPARE statement FROM @rowCounts;
EXECUTE statement;
DEALLOCATE PREPARE statement;
ELSE
# if no base tables found then return an empty set
select 1 where 0 = 1;
END IF;
END$$
DELIMITER ;
Select group_concat(Query SEPARATOR ' union all ') as Full_Query from (SELECT CONCAT('SELECT ''',table_name,''', COUNT(*) FROM ', table_name) as Query
FROM information_schema.tables) AS T1 into @sql from (select
table_schema db,
table_name tablename from information_schema.tables where table_schema not in
('performance_schema', 'mysql', 'information_schema')) t;
那就快跑
prepare s from @sql; execute s; deallocate prepare s;