2

I'm working on a query and my table is very large, many columns. I do not want to type out each individual column in the where clause- is there a way where I can select all without writing them, something like this (below does not work):

select *
from table t
where t.* is not NULL
lucyb
  • 333
  • 5
  • 15

2 Answers2

3

You can try to find fields from information_schema.columns table then execute dynamic sql.

like this.

SET @Efields := '';
SET @Esql:='';
SET @Table_Name = 'table1';

select @Efields := GROUP_CONCAT(COLUMN_NAME SEPARATOR ' IS NOT NULL AND ') 
from information_schema.columns
where TABLE_NAME = @Table_Name;
SELECT @Esql:=CONCAT('select * from ',@Table_Name ,'  WHERE ',@Efields,' IS NOT NULL ');

PREPARE stmt1 FROM  @Esql; 
EXECUTE stmt1; 
DEALLOCATE PREPARE stmt1;

sqlfiddle:https://www.db-fiddle.com/f/32PisE5bChKSVPzk6LjEP3/0

D-Shih
  • 44,943
  • 6
  • 31
  • 51
1

An alternative, if you wanted, in your where clause do something like (assuming your number of columns in the table is 10):

Select T.*
from [TABLE] T
where 1|2|3|4|5|6|7|8|9|10 is not null
DeFlanko
  • 66
  • 8