1

I have a table that contains amongst other things a datetime column, and 4 other columns that contain either a zero or a number. I would like to return how many columns in a row contain a zero in mysql.

for example

1_datetime 0 2 5 0

2_datetime 5 6 3 0

I would like for each datetime to know how many columns have zeros in

so i want

1_datetime 2

2_datetime 1

thanks

srh
  • 25
  • 1
  • 4

1 Answers1

2

The easiest way is to sum negated values:

SELECT datetime, (!a) + (!b) + (!c) + (!d) AS zero_count
  FROM table
. . . . 

Operator ! returns 1 if operand is 0, 0 if operand is non-zero, and NOT NULL if operand is NULL.

Parenthsis added because adding + have higher precedence over negation.

Kondybas
  • 4,323
  • 15
  • 13