SELECTCONCAT(GROUP_CONCAT(CONCAT('SELECT \'', table_name, '\' table_name,COUNT(*) rows FROM ', table_name) SEPARATOR ' UNION '),' ORDER BY table_name')INTO @sql FROM table_list;
在此查询中,table_list是第一步中查询结果的表名列表。
以下查询使用第一个查询作为派生表,并以字符串形式返回SQL语句。
SELECTCONCAT(GROUP_CONCAT(CONCAT('SELECT \'', table_name, '\' table_name,COUNT(*) rows FROM ', table_name) SEPARATOR ' UNION '),' ORDER BY table_name')INTO @sql FROM (SELECT table_nameFROM information_schema.tablesWHERE table_schema ='yiibaidb'AND table_type ='BASE TABLE') table_list;
WITH table_list AS (SELECT table_nameFROM information_schema.tables WHERE table_schema ='yiibaidb'AND table_type ='BASE TABLE') SELECTCONCAT( GROUP_CONCAT(CONCAT("SELECT '",table_name,"' table_name,COUNT(*) rows FROM ",table_name) SEPARATOR " UNION "),
' ORDER BY table_name' )INTO @sqlFROM table_list;