Vitalynx Vitalynx - 1 year ago 62
MySQL Question

SQL specific count query

"SELECT COUNT (one, two, three, four, five) FROM $this->tablename where username='$username' and one='Not done' and two='Not done' and three='Not done' and four='Not done' and five='Not done'"

I want to count the amount of times the value for "Not done" is found for row "username" for the columns one, two, three, four and five. This query however, does not work. I have no clue to make it work, but I do have a feeling that this query could be way shorter.

Answer Source

Try this one:

SELECT (one='Not done')
     + (two='Not done')
     + (three='Not done')
     + (four='Not done')
     + (five='Not done')
     AS num_not_done
FROM $this->tablename 
where username='$username'

The expression (column='Not done') will return 1 or 0. So you can just sum the results to get the count of matches.