Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

mysql - Select column names whose entries are not null

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 column names for which the following returns at least one entry:

SELECT DISTINCT column_name FROM table WHERE column_name IS NOT NULL

I tried the following:

SELECT column_name
FROM information_schema.columns
WHERE table_name = "table_name"
AND EXISTS (
    SELECT DISTINCT column_name FROM table_name WHERE column_name IS NOT NULL
)

But this also returns the column names where all the entries are NULL.

So how do I get only those columns with non-NULL entries?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...