4

或者换一种说法:如何找到表中的哪条记录具有最少的空字段或空白字段?

无需单独计算每个字段 - 该表有 161 个字段。

4

2 回答 2

2

这可以通过构造一个动态查询来完成,例如:

SELECT id,
       (IF(col1 = NULL OR col1 = "", 1, 0) +
        IF(col2 = NULL OR col3 = "", 1, 0) +
        ...
        IF(coln = NULL OR coln = "", 1, 0)
       ) AS null_count
FROM table_name
ORDER BY null_count DESC
LIMIT 1;

这可以通过使用INFORMATION_SCHEMA.COLUMNS然后执行形成一个新的动态查询来轻松完成dynamic sql query。此外,您可能需要通过将会话GROUP_CONCAT级别变量设置group_concat_max_len为更高的值来增加函数输出的最大长度。

SET GLOBAL group_concat_max_len = 4294967295;

SELECT @query1 := CONCAT('SELECT id,
                                 (',
                         GROUP_CONCAT(CONCAT('IF(',COLUMN_NAME,' IS NULL OR ',
                                                   COLUMN_NAME,' = "", 1, 0
                                                ) 
                                           ') 
                                      SEPARATOR ' + '),
                                ') AS null_count
                         FROM table_name
                         ORDER BY null_count DESC
                         LIMIT 1')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = SCHEMA()
      AND TABLE_NAME = 'table_name';

PREPARE stmt FROM @query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;

示例:SQLFiddle

于 2012-09-11T10:22:52.097 回答
1

免责声明:我的问题错了,以为 OP 正在寻找具有最多 NULL 的表的列。尽管如此,也许它对任何人都有用。

像这样创建一个过程:

drop procedure if exists test_most_pop_field;
DELIMITER $$
CREATE PROCEDURE test_most_pop_field(IN tableName varchar(100))
BEGIN

DECLARE done INT DEFAULT 0;
DECLARE sql_query VARCHAR(255);

DECLARE cur CURSOR FOR 
SELECT CONCAT('INSERT INTO tmp_result(columnName, numberOfEmptyRows) SELECT "', COLUMN_NAME, '" AS columnName, SUM(IF(',COLUMN_NAME,' IS NULL OR ', COLUMN_NAME,' = "", 1, 0)) AS numberEmptyRows FROM ', TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = SCHEMA()
AND TABLE_NAME = tableName;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

DROP TABLE IF EXISTS tmp_result;
CREATE TEMPORARY TABLE tmp_result(columnName varchar(100), numberOfEmptyRows int);

OPEN cur;

REPEAT
  FETCH cur INTO sql_query;
  IF NOT done THEN
    BEGIN
      SET @sql = sql_query; /*this extra step is necessary, cause otherwise it's a syntax error, don't ask me why*/
      PREPARE stmt FROM @sql; 
      EXECUTE stmt; 
      DEALLOCATE PREPARE stmt;
    END;
  END IF;
UNTIL done END REPEAT;

CLOSE cur;

SELECT * FROM tmp_result ORDER BY numberOfEmptyRows DESC /*optionally LIMIT 1*/;

END $$
DELIMITER ;

然后使用您要检查的表名调用它:

CALL test_most_pop_field('yourTableName');
于 2012-09-11T12:43:44.277 回答