Select column names whose entries are not null

前端 未结 2 502
心在旅途
心在旅途 2020-12-21 23:43

I would like to have a list of those columns of a table that have at least one non-NULL data entries in them.

In other words, I would like to get the co

2条回答
  •  失恋的感觉
    2020-12-21 23:55

    Create from the INFORMATION_SCHEMA.COLUMNS table a string that contains the SQL you wish to execute, then prepare a statement from that string and execute it.

    The SQL we wish to build will look like:

      SELECT 'column_a'
      FROM   table_name
      WHERE `column_a` IS NOT NULL
      HAVING COUNT(*)
    UNION ALL
      SELECT 'column_b'
      FROM   table_name
      WHERE `column_b` IS NOT NULL
      HAVING COUNT(*)
    -- etc.
    

    (One could omit the WHERE clause and substitute COUNT(*) for COUNT(column), but I think that might be less efficient on indexed columns).

    This can be done using the following:

    SET group_concat_max_len = 4294967295;
    
    SELECT GROUP_CONCAT(
     ' SELECT ',QUOTE(COLUMN_NAME),
     ' FROM   table_name',
     ' WHERE `',REPLACE(COLUMN_NAME, '`', '``'),'` IS NOT NULL',
     ' HAVING COUNT(*)'
    SEPARATOR ' UNION ALL ')
    INTO   @sql
    FROM   INFORMATION_SCHEMA.COLUMNS
    WHERE  TABLE_SCHEMA = DATABASE()
       AND TABLE_NAME = 'table_name';
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See it on sqlfiddle.

提交回复
热议问题