Maybe my question isn't so clear, please let me explain: What I need is to get a list of all users along with a corresponding client number/wholesaler combination, every client has 1 up to 4 different client number/wholesaler combo
In my db I have 2 tables
USERS
id | name
---------
1 | a
2 | b
CLIENT_NUMBERS
id | user_id | number | wholesaler
----------------------------------
1 | 1 | ac1 | aw1
2 | 1 | ac2 | aw2
3 | 2 | bc1 | bw1
Using a simple INNER JOIN I got client rows duplicated, one for each corresponding client number/wholesaler
I've managed to fix the results using GROUP_CONCAT in this query:
SELECT a.id AS user_id, a.name AS Name
GROUP_CONCAT(b.client_no, ', ', b.wholesaler SEPARATOR '; ') AS client_no_wholesaler
FROM users AS a
INNER JOIN client_numbers AS b ON a.id = b.user_id
GROUP BY ID
user_id | name | client_no_wholesaler
--------------------------------------------
1 | a | ac1, aw1; ac2, aw2
2 | b | bc1, bw1
So far so good, but I need to "explode" the client number/wholesaler combination into different columns so my results can look like this:
user_id | name | client_no_wholesaler1 | client_no_wholesaler2 | ...up to 4
----------------------------------------------------------------------------
1 | a | ac1, aw1 | ac2, aw2 |
2 | b | bc1, bw1 | |
Doing this after getting the query results with a simple PHP explode is not an option because I'm using a class to generate a XLS file and its based on my query result columns, any ideas will be appreciated.