I have two tables: `jobs' & 'clients'.
The clients
table looks like this:
/----+------\
| id | name |
+----+------+
| 1 | Ben |
| 2 | Max |
\----+------/
And the jobs
table:
/----+-----------+--------\
| id | client_id | status |
+----+-----------+--------+
| 1 | 1 | alpha |
| 2 | 1 | beta |
| 3 | 1 | beta |
| 4 | 2 | beta |
\----+-----------+--------/
I'm looking to create a statement that will return the name of the client along with the number of times each status appears, like this:
/------+-------+------\
| name | alpha | beta |
+------+-------+------+
| Ben | 1 | 2 |
| Max | 0 | 1 |
\------+-------+------/
I do not require there to be a 0 where no values exist though.
I have tried SELECT name, (SELECT status FROM jobs WHERE client_id = clients.id) FROM clients
but it returns more than one row in the sub-query.