I’m using this MySQL query
SELECT COUNT(pros_a) + COUNT(pros_b) + COUNT(pros_c) + COUNT(pros_d) + COUNT(pros_e) AS pros_total FROM my_table WHERE contentid = 'id'
to count and sum fields in each column. But this returns ALL fields (full and empty). I want to count only filled fields, excluding empty fields. How can I do that? Just a little bit… confused! 🙂
Any help would be very appreciated!
Thanks
EDIT:
my_table
pros_a pros_b pros_c pros_d pros_e
------- ------ ------ ------ ------
good (empty) good good (empty)
expected result
pros_total
----------
3
The
COUNT(column)function does not count fields that areNULL. It counts empty (zero-length) strings though.You can change these into:
or:
The last works in MySQL because
TRUEis evaluated as1andFALSEas0.