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 6931837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:37:45+00:00 2026-05-27T11:37:45+00:00

I have a typical N-M relationship structure like this: two independent tables: Table Client:

  • 0

I have a typical N-M relationship structure like this:

two independent tables:

Table Client:
id | name
1  | Joe
2  | Marc

and

Table Product:
id | name
1  | Toyota
2  | Ford
3  | Fiat

and a linking table like:

Table Purchase:
idClient | idProduct | Paid
1        | 1         | 3000
1        | 2         | 2999
2        | 3         | 4500
2        | 1         | 1000

I would like a query that produces:

client | Toyota | Ford | Fiat
Joe    |  3000  | 2999 | 
Marc   |  1000  |      | 4500

is there some way to do it in MySQL?

Thanks !

  • 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-05-27T11:37:45+00:00Added an answer on May 27, 2026 at 11:37 am

    Like @StuartAinsworth said, you need to do a Pivot Query.


    For a dynamic approach use this:

    create table Client (
    id int,
    name varchar(10)
    )
    
    insert into Client values (1, 'Joe')
    insert into Client values (2, 'Marc')
    
    create table Product (
    id int,
    name varchar(10)
    )
    
    insert into Product values (1, 'Toyota')
    insert into Product values (2, 'Ford')
    insert into Product values (3, 'Fiat')
    
    create table Purchase (
    idClient int,
    idProduct int,
    Paid int
    )
    
    insert into Purchase values (1, 1, 3000)
    insert into Purchase values (1, 2, 2999)
    insert into Purchase values (2, 3, 4500)
    insert into Purchase values (2, 1, 1000)
    
    
    declare @cmd varchar(2048)
    declare @prod varchar(10), @count int, @total int
    
    select @cmd = 'select c.name, '
    
    select @count = 1, @total = max(id) from Product
    
    while @count <= @total
    begin
        select @prod = name from Product where id = @count
    
        if(@count <> @total)
        begin
            select @cmd = @cmd + 'sum(CASE WHEN p.name = "' + @prod + '" THEN u.Paid ELSE 0 END) as "' + @prod + '", ' 
        end
        else   
        begin
            select @cmd = @cmd + 'sum(CASE WHEN p.name = "' + @prod + '" THEN u.Paid ELSE 0 END) as "' + @prod + '" ' 
        end
        select @count = @count + 1
    end
    
    select @cmd = @cmd + 'from Client c ' +
     + 'inner join Purchase u on u.idClient = c.id ' +
     + 'inner join Product p on p.id = u.idProduct ' +
     + 'group by c.name'
    
    
    exec(@cmd)
    
    drop table Client
    drop table Product
    drop table Purchase
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have this table structure: Patient -> PatientTag -> Tag A typical N:M
I have a typical Subversion set-up: /trunk /branches/client-one /branches/client-two /branches/client-three Trunk holds main development
Taking the typical products / categories many-to-many relationship you'd typically have a relationship like
I have two tables, A and B and a simple table, X, that maintains
I have a typical table, e.g. id(int) name(varchar) address(varchar) date(datetime) I also have a
I have a typical login screen(table layout). What I would like to do is
We have a typical business application with an Outlook-looking Winforms client talking to asmx
I have this typical scenario. I have a smartclient application built on .net 2.0
I have a typical User model (username, password, name, etc). I want to allow
I have the typical HTML "contact me" page, i.e. name, e-mail address and message.

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.