选择非空的列/段

时间:2014-09-02 14:52:04

标签: mysql

我有以下内容,这是来自此url的stackoverflow搜索的改编: Find all those columns which have only null values, in a MySQL table

SELECT CONCAT(
         'SELECT * FROM ('
       ,  GROUP_CONCAT(
            'SELECT ', QUOTE(TABLE_NAME), ' AS `table`,'
          , 'IF('
          ,   'COUNT(`', REPLACE(COLUMN_NAME, '`', '``'), '`),'
          ,   'NULL,'
          ,    QUOTE(COLUMN_NAME)
          , ') AS `column` '
          , 'FROM `', REPLACE(TABLE_NAME, '`', '``'), '`'
          SEPARATOR ' UNION ALL '
         )
       , ') t WHERE `column` IS NOT NULL'
       )
INTO   @sql
FROM   INFORMATION_SCHEMA.COLUMNS
WHERE  TABLE_NAME = 'about';

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

结果:

+--------------+ 
| table column | 
+--------------+ 
| ----- -----  | 
| about par6   | 
| about par7   | 
| about par8   | 
| about par9   | 
| about par10  | 
+--------------+ 

这会显示两列:表名和列值。 我怎样才能获得非空的列并从数据库中检索它们的值?

0 个答案:

没有答案