mysql - 遍历列并在 MySQL 中的每列上运行频率(计数*)

标签 mysql loops dynamic frequency

我正在尝试编写一个脚本来进行如下简单查询:

select col1, count(*)
from city
group by col1
order by 2 desc

并且对每一列都这样做。理想情况下,如果结果是并排的(可以合并列吗?)

编辑:

我想通了。花了 3.5 小时,但我到了那里。第一次使用存储过程和动态 SQL,所以这是一个很好的学习经验。

---- This stored procedure takes the database name and table name and will run a frequency on each column.

DELIMITER $$
 DROP PROCEDURE IF EXISTS Data_Audit$$
 CREATE PROCEDURE Data_Audit(db_name varchar(100), tbl_name varchar(100))
       BEGIN         
                -- set up variables
               DECLARE col_number  int;
                    DECLARE sql_code varchar(255);            
               SET col_number =  0;
               SET @total_rows = 0;
               SET @col_count = 0;


               -- Set variable to number of columns. Used for number of loops
               SELECT COUNT(*)
               into @col_count
                    FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE table_schema = db_name 
                    AND table_name = tbl_name;


                    -- Set variable to number of rows           
                    set @row_count_code = concat ('set @row_count = (select count(*) FROM ',tbl_name,')');                  

                    PREPARE stmt FROM @row_count_code;
                    EXECUTE stmt;
                    DEALLOCATE PREPARE stmt; 


                    set @drop_new_table = concat('
                    drop table if exists ',
                    db_name,
                    '.',
                    tbl_name,
                    '_frequency;');


                    PREPARE stmt FROM @drop_new_table;
                    EXECUTE stmt;
                    DEALLOCATE PREPARE stmt; 

                    -- Create basic table structure 

                    set @create_new_table = concat('
                    create table ',
                    db_name,
                    '.',
                    tbl_name,
                    '_frequency',
                    ' (Column_Value varchar(100), Frequency varchar(100),Frequency_percent varchar(50))'
                    ';');   


                    -- select @create_new_table                 
                    PREPARE stmt FROM @create_new_table;
                    EXECUTE stmt;
                    DEALLOCATE PREPARE stmt;      


                    -- Loop through columns
               WHILE col_number  < @col_count DO
                            SET  col_number = col_number + 1;
                           -- SET @sql = NULL;
                          --  set @col_num = col_number;
                                    SELECT column_name 
                                     INTO @sql_code
                                      FROM INFORMATION_SCHEMA.COLUMNS
                                     WHERE table_schema = db_name
                                       AND table_name = tbl_name
                                       AND ordinal_position = col_number;


                                     -- This is the main query. Inserts each time into the new table 
                                     SET @sql = CONCAT('insert into ',
                                                             db_name,
                                                              '.',
                                                              tbl_name,
                                                              '_frequency Select ''', upper(@sql_code), ''',''Frequency'',''Frequency_percent'' UNION ALL (SELECT `',
                                                             @sql_code,
                                                             '` , count(*), concat(truncate((count(*) / ',
                                                             @row_count,
                                                             ') *100,2),''%'') as Frequency FROM ',
                                                             tbl_name,
                                                             ' group by `',
                                                             @sql_code,
                                                             '` order by 2 desc limit 30) UNION ALL Select '''','''',''''');                                


                                    PREPARE stmt FROM @sql;
                                    EXECUTE stmt;
                                    DEALLOCATE PREPARE stmt;                


              END WHILE;       

     END$$
   DELIMITER ;


call Data_Audit('world','country');

最佳答案

我想通了。花了 3.5 小时,但我到了那里。第一次使用存储过程和动态 SQL,所以这是一个很好的学习经验。

---- This stored procedure takes the database name and table name and will run a frequency on each column.

DELIMITER $$
 DROP PROCEDURE IF EXISTS Data_Audit$$
 CREATE PROCEDURE Data_Audit(db_name varchar(100), tbl_name varchar(100))
       BEGIN         
                -- set up variables
               DECLARE col_number  int;
                    DECLARE sql_code varchar(255);            
               SET col_number =  0;
               SET @total_rows = 0;
               SET @col_count = 0;


               -- Set variable to number of columns. Used for number of loops
               SELECT COUNT(*)
               into @col_count
                    FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE table_schema = db_name 
                    AND table_name = tbl_name;


                    -- Set variable to number of rows           
                    set @row_count_code = concat ('set @row_count = (select count(*) FROM ',tbl_name,')');                  

                    PREPARE stmt FROM @row_count_code;
                    EXECUTE stmt;
                    DEALLOCATE PREPARE stmt; 


                    set @drop_new_table = concat('
                    drop table if exists ',
                    db_name,
                    '.',
                    tbl_name,
                    '_frequency;');


                    PREPARE stmt FROM @drop_new_table;
                    EXECUTE stmt;
                    DEALLOCATE PREPARE stmt; 

                    -- Create basic table structure 

                    set @create_new_table = concat('
                    create table ',
                    db_name,
                    '.',
                    tbl_name,
                    '_frequency',
                    ' (Column_Value varchar(100), Frequency varchar(100),Frequency_percent varchar(50))'
                    ';');   


                    -- select @create_new_table                 
                    PREPARE stmt FROM @create_new_table;
                    EXECUTE stmt;
                    DEALLOCATE PREPARE stmt;      


                    -- Loop through columns
               WHILE col_number  < @col_count DO
                            SET  col_number = col_number + 1;
                           -- SET @sql = NULL;
                          --  set @col_num = col_number;
                                    SELECT column_name 
                                     INTO @sql_code
                                      FROM INFORMATION_SCHEMA.COLUMNS
                                     WHERE table_schema = db_name
                                       AND table_name = tbl_name
                                       AND ordinal_position = col_number;


                                     -- This is the main query. Inserts each time into the new table 
                                     SET @sql = CONCAT('insert into ',
                                                             db_name,
                                                              '.',
                                                              tbl_name,
                                                              '_frequency Select ''', upper(@sql_code), ''',''Frequency'',''Frequency_percent'' UNION ALL (SELECT `',
                                                             @sql_code,
                                                             '` , count(*), concat(truncate((count(*) / ',
                                                             @row_count,
                                                             ') *100,2),''%'') as Frequency FROM ',
                                                             tbl_name,
                                                             ' group by `',
                                                             @sql_code,
                                                             '` order by 2 desc limit 30) UNION ALL Select '''','''',''''');                                


                                    PREPARE stmt FROM @sql;
                                    EXECUTE stmt;
                                    DEALLOCATE PREPARE stmt;                


              END WHILE;       

     END$$
   DELIMITER ;


call Data_Audit('world','country');

关于mysql - 遍历列并在 MySQL 中的每列上运行频率(计数*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711675/

相关文章:

MySQL 从 5.7 升级到 8.0 失败,在步骤 EXEC 生成 mysql-systemd-start 时失败

mysql - 选择每组的最大行数

javascript - Highcharts 多线图

r - 识别 R 中的模式,相应地计算和分配值

android: 单击 TableRow 时调用方法

php - HTML 复选框字段被传递给 PHP 作为选中状态,即使它没有被选中

javascript 中断并继续循环

javascript - 如何在 string.match 中添加循环值?

css - 使用 vue/nuxt 将样式属性附加到所有( anchor )元素

c# - 从 c# expando 对象动态读取属性