Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tidiest way to filter out rows where all columns = a value

I have a query with loads of columns. I want to select rows where not all the columns are equal to 0.

select * from table
where 
not
( column1 = 0 and
  column2 = 0 and
  column3 = 0 and
  ...
  column45 = 0)

Is this really the tidiest way to do it?

Supposing I then need to change it to ignore when all columns are 1, or negative.. Its a lot of cut and paste..

like image 250
Mongus Pong Avatar asked Jan 19 '26 08:01

Mongus Pong


1 Answers

It appears as though the 45 individual columns have a similar meaning. As such, I would encourage you to properly normalize this table. If you did, the query would be simpler and would likely perform better.

like image 79
George Mastros Avatar answered Jan 20 '26 21:01

George Mastros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!