Consider a table with a status
field that holds one of two values:
| status | set('active','default') | YES | | active |
I need to know how many records are in the table, and additionally how many have the status
set to active
. I notice that SUM
returns 1
for each record set to active
and 2
for each record set to default
:
mysql> select count(*), sum(status) from users;
+----------+-------------+
| count(*) | sum(status) |
+----------+-------------+
| 3 | 4 |
+----------+-------------+
1 row in set (0.00 sec)
mysql> select status from users;
+---------+
| status |
+---------+
| default |
| active |
| active |
+---------+
3 rows in set (0.00 sec)
Can I rely on this behaviour? Is this a bad practice?