Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8624581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:33:54+00:00 2026-06-12T07:33:54+00:00

Maybe my question isn’t so clear, please let me explain: What I need is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:33:55+00:00Added an answer on June 12, 2026 at 7:33 am

    What you want is generally called a “pivot“.

    Here’s how you code it in sql using an extra join for each extra column:

    SELECT
        a.id AS user_id,
        a.name AS Name,
        CONCAT(b1.client_no, ', ', b1.wholesaler) AS client_no_wholesaler1,
        CONCAT(b2.client_no, ', ', b2.wholesaler) AS client_no_wholesaler2,
        CONCAT(b3.client_no, ', ', b3.wholesaler) AS client_no_wholesaler3,
        CONCAT(b4.client_no, ', ', b4.wholesaler) AS client_no_wholesaler4
    FROM users AS a
    JOIN client_numbers AS b1 ON b1.user_id = a.id
    LEFT JOIN client_numbers AS b2 ON b2.user_id = a.id and b2.id > b1.id
    LEFT JOIN client_numbers AS b3 ON b3.user_id = a.id and b3.id > b2.id
    LEFT JOIN client_numbers AS b4 ON b4.user_id = a.id and b4.id > b3.id
    GROUP BY 1, 2
    

    Note that the extra joins avoid duplicate joins by the addition of an ever-increasing id condition in the ON clause for the extra joins. If id isn’t a suitable ordering column, chose something else to separate the joins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe the question is familiar but I need an explanation in this case. Let's
Maybe the question isn't that clear, but with an example, I guess I'll clear
I know the question title isn't the best. Let me explain. I do a
If my question isn't clear, please help me improve it with comments. I'm new
The question may at first sound silly, but maybe it isn't at all. Java
First of all, my question isn't really specific to C# or XNA, but my
Maybe this isn't even a valid question, so any help would be good.
I have maybe dump question but it can't get it off my mind. I'm
This maybe a really simple question but for some reason my code isn't working.
I'm just totally confused with lists and monads, so maybe my question isn't correct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.