(Little late answer :), but it may help someone else !)
First, you must realize that a single FROM
can't generate a row with both c1 and s1 (for example), and another row with both c1 and s2. Except, of course, a single row with GROUP_CONCAT(client), GROUP_CONCAT(staff)
, but it wouldn't be useful, as it loses the link between clients and staffs.
What you have to do is to join your table with itself :
client staff code
------------------------
c1 null code1
c2 null code1
null s1 code1
null s2 code1
c3 null code1*
c4 null code2*
SELECT c.client, s.staff, c.code
FROM `table` c
INNER JOIN `table` s ON s.code = c.code
client staff code
------------------------
c1 null code1
c1 null code1
c1 s1 code1
c1 s2 code1
c2 null code1
c2 null code1
c2 s1 code1
c2 s2 code1
null null code1
null null code1
null s1 code1
null s2 code1
null null code1
null null code1
null s1 code1
null s2 code1
Obviously, the cross join between two 4-rows tables give 16 result. Excluding rows with null :
SELECT c.client, s.staff, c.code
FROM `table` c
INNER JOIN `table` s ON s.code = c.code AND s.staff IS NOT NULL
WHERE c.client IS NOT NULL
client staff code
------------------------
c1 s1 code1
c1 s2 code1
c2 s1 code1
c2 s2 code1
Then, you can group by staff and code, and aggregates the clients :
SELECT GROUP_CONCAT(c.client), s.staff, c.code
FROM `table` c
INNER JOIN `table` s ON s.code = c.code AND s.staff IS NOT NULL
WHERE c.client IS NOT NULL
GROUP BY staff, c.code
client staff code
------------------------
c1,c2 s1 code1
c1,c2 s2 code1
NB : As written, it won't show the codes having only clients and not staffs, and those having staffs and not codes. If one of this case is possible, then you have to LEFT JOIN
the concerned table (table c LEFT JOIN staff s
, or table s LEFT JOIN table c
, with the appropriate tests on NULL) instead of INNER JOIN.
client staff code time1 time2
------------------------
c1 null code1
c2 null code1
null s1 code1
null s2 code1
*c3 null code2*
*null s3 code3*
SELECT GROUP_CONCAT(c.client), s.staff, c.code
FROM `table` c
LEFT JOIN `table` s ON s.code = c.code AND s.staff IS NOT NULL
WHERE c.client IS NOT NULL
GROUP BY staff, c.code
client staff code
------------------------
c1,c2 s1 code1
c1,c2 s2 code1
c3 NULL code2
SELECT GROUP_CONCAT(c.client), s.staff, c.code
FROM `table` s
LEFT JOIN `table` c ON c.code = s.code AND c.client IS NOT NULL
WHERE s.staff IS NOT NULL
GROUP BY staff, c.code
client staff code
------------------------
c1,c2 s1 code1
c1,c2 s2 code1
NULL s3 code3
It's not clear why you have rows that have either client or staff filled (but not both); maybe you should rethink your model.