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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:38:33+00:00 2026-05-25T18:38:33+00:00

I have 3 tables Labels UserValues Users Labels looks like this: 1|First Name 2|Last

  • 0

I have 3 tables

Labels

UserValues

Users

Labels looks like this:

1|First Name
2|Last Name
3|Favorite Book
etc...

Users looks like this:

1|[phone_number]
2|[phone_number]
etc....

UserValues looks like this:

1|  [label_id_for_First_Name]  |John      |  [user_id]
2|  [label_id_for_Last_Name]   |Smith     |  [user_id]
3|  [label_id_for_Fav_Book]    |Moby Dick |  [user_id]
etc...

some users may have not filled in some fields (all are optional except for the phone number that is used as a primary key).

I’m stumped as to how I can write a query that would flatten this data to look like:

uid      |First_Name|Last_Name | Favorite_Book
[user_id]|John      |Smith     | Moby Dick //user has all fields filled in
[user_id]|Mary      | [null]   | Kite Runner //user didn't have a last name

The idea is that it would grow in column width for as many columns as there were labels associated with these particular users.

I’d like to select on the users and have 1 row per user with all the values going out to the right.

I can see how I can do this in several queries, but I was hoping to learn what the right way to do this is (maybe it IS to do it in several queries, but I suspect it’s not).

TIA.

  • 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-25T18:38:33+00:00Added an answer on May 25, 2026 at 6:38 pm

    If you want to write a query using the current data and giving the result in the example this would be it:

    SQL coding horror

    SELECT 
      u.id as user_id
      , uvfirstname.value as firstname
      , uvlast_name.value as lastname
      , uvbook.value as book
    FROM users u
    LEFT JOIN uservalues uvfirstname 
           ON (uvfirstname.label_id =
                 (SELECT l1.id FROM label l1 WHERE l1.name = 'First name')
               AND uvfirstname.user_id = u.id)
    LEFT JOIN uservalues uvlastname 
           ON (uvlastname.label_id = 
                 (SELECT l2.id FROM label l2 WHERE l2.name = 'Last name')
               AND uvlastname.user_id = u.id)
    LEFT JOIN uservalues uvbook
           ON (uvbook.label_id = 
                 (SELECT l3.id FROM label l3 WHERE l3.name = 'Fav book')
               AND uvbook.user_id = u.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have a JPanel subclass on which I add buttons, labels, tables, etc. To
I have several components on my UIView like textfields, labels, tables, buttons. I want
I'm using phpactiverecord for this project, I have this db structure: Tables: Tickets, Labels
I have a table that looks like the following: <table class=theClass> <tr> <td class=anotherClass><strong>Label1:</strong></td>
I have a form like this: <form action=form_send.php method=post> <table> <tr> <td><label for=address>address</label></td> <td><input
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have tables linked by FK, I query on the first table using entity
I have 2 tables. 1st,cardb ID,car,engine,gear 2nd,labels ID,label,value what i need to do is,
So often you will have a number of labels (lets use name, age, colour)

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.