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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:58:55+00:00 2026-06-16T14:58:55+00:00

Ok, I’m still a beginner in sql and can not figure this one out

  • 0

Ok, I’m still a beginner in sql and can not figure this one out yet.
I have four tables: companies, persons, details, person_details.

companies: 
id, compname
(1, ACME), 
(2, ACME Group), ...

persons: 
id, name, lastname, company id
(1, donald, duck, 1), 
(2, lucky, luke, 1), 
(3, mickey, mouse, 2)

details: 
id, description
(1, 'weight'), 
(2, 'height'), 
(3, 'haircolor'), ...

person_details: 
id, persons id, details id, value
(1, 1, 1, 70), 
(2, 1, 3, 'red'), 
(3, 2, 1, 90), 
(4, 3, 2, 180)

As you can see, not all persons have all the details and the list of available details is variable.

Now, for a given arary of person ids and detail ids, I would like to get rows containing: company name and id, person name and last name, detail name and value for each of the details in the supplied array.
Let’s say persons(1,2), details(1,3) should result in:

companies.id, companies.name, name, lastname, details.description, person_details.value,...    
    1, ACME, donald, duck, 'weight', 70, 'haircolor', 'red'
    2, ACEM, lucky, luke, 'weight', 90, 'haircolor', null

Help, please…

  • 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-16T14:58:57+00:00Added an answer on June 16, 2026 at 2:58 pm

    Based on your description it seems like you want to pivot the data but unfortunately MySQL does not have a pivot function so you will need to replicate it using an aggregate function with a CASE statement.

    If you know the description values ahead of time you can hard-code your query to the following:

    select c.id,
      c.compname,
      p.name,
      p.lastname,
      max(case when d.description = 'weight' then pd.value end) weight,
      max(case when d.description = 'haircolor' then pd.value end) haircolor,
      max(case when d.description = 'height' then pd.value end) height
    from companies c
    left join persons p
      on c.id = p.`company id`
    left join person_details pd
      on p.id = pd.`persons id`
    left join details d
      on pd.`details id` = d.id
    -- where p.id in (1, 2)
    group by c.id, c.compname, p.name, p.lastname
    

    See SQL Fiddle with Demo

    If you have an unknown number of values, then you can use a prepared statement to generate this dynamically similar to this:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'MAX(CASE WHEN d.description = ''',
          description,
          ''' then pd.value end) AS ',
          description
        )
      ) INTO @sql
    FROM details;
    
    SET @sql = CONCAT('SELECT c.id,
                          c.compname,
                          p.name,
                          p.lastname, ', @sql, ' 
                      from companies c
                      left join persons p
                        on c.id = p.`company id`
                      left join person_details pd
                        on p.id = pd.`persons id`
                      left join details d
                        on pd.`details id` = d.id
                      -- where p.id in (1, 2)
                      group by c.id, c.compname, p.name, p.lastname');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See SQL Fiddle with Demo

    Both versions generate the result:

    | ID |   COMPNAME |   NAME | LASTNAME | WEIGHT | HEIGHT | HAIRCOLOR |
    ---------------------------------------------------------------------
    |  1 |       ACME | donald |     duck |     70 | (null) |       red |
    |  1 |       ACME |  lucky |     luke |     90 | (null) |    (null) |
    |  2 | ACME Group | mickey |    mouse | (null) |    180 |    (null) |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.