Question

I have a table with 46 columns in my database, In these 46 columns 25 or 26 are mostly empty. when I select all columns by some condition I want only those column will select which have some data in it

No correct solution

OTHER TIPS

You can try this query to show you the not null columns' names:

SET group_concat_max_len = 4294967295;

SELECT GROUP_CONCAT(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; 

SQL Fiddle

And then just excute these columns manualy:

 SELECT column_name3, column_name8 FROM Table_Name;

SQL Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top