I have the following table:
+----+----------+------------+
| id | drink | gender |
+----+----------+------------+
| 1 | Beer | m |
| 2 | Milk | f |
| 3 | Milk | f |
| 4 | Tea | m |
+----+----------+------------+
Now I want to count which gender prefers which drink. The result should look like this:
+-------+-------+--------+
| drink | fem | male |
+-------+-------+--------+
| Beer | 0 | 1 |
| Milk | 2 | 0 |
| Tea | 0 | 1 |
+----+----------+--------+
Has anyone an idea?
Thanks in advance for your suggestions.
SQLFiddle Demo
the query above can be further be converted into prepared statement. This is helpful, if for instance, you manage to add another value for the gender (eg.
ufor unisex) without modifying the original query. example,SQLFiddle Demo