I have a query that returns exactly what I want when there is only one type of group by response. When there is more than one I get the "Subquery returns more than 1 row" error.
I've tried multiple ways of structuring the query including using "in" but couldn't get anything to work.
SELECT
(
SELECT substring(postcode, 1, locate (' ', postcode) - 1)
FROM user_locations AS ul
JOIN users AS u ON u.id = ul.user_id
WHERE u.membertype = "customer"
) AS postcode,
(
SELECT count(u.membertype)
FROM users AS u
JOIN user_locations AS ul ON u.id = ul.user_id
WHERE u.membertype = "cook"
GROUP BY ul.postcode
) AS cook,
(
SELECT count(u.membertype)
FROM users AS u
JOIN user_locations AS ul ON u.id = ul.user_id
WHERE u.membertype = "customer"
GROUP BY ul.postcode
) AS customer
where I have multiple postcodes I would expect to see
Postcode | Cooks | Customers
G83 | 12 | 34
G84 | 19 | 76
G85 | 10 | 50