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

  • Home
  • SEARCH
  • 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 8523363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:23:19+00:00 2026-06-11T07:23:19+00:00

Table 1 : User_Profile ——————————————————————————————- User_profile_id | Profile_form_id | Email | Zipcode | Firstname

  • 0

Table 1 : User_Profile

-------------------------------------------------------------------------------------------
User_profile_id   | Profile_form_id |   Email       |   Zipcode   |   Firstname | Lastname
-------------------------------------------------------------------------------------------

1                 | 4               |john@gmail.com |  123456     |  john       | peter 
------------------------------------------------------------------------------------------

–

Table 2 : Profile_attribute_form

-------------------------------------------------------------------
profile_attribute_id   | Profile_attribute_name |  profile_form_id 
-------------------------------------------------------------------

1                      | Address                |  4
-------------------------------------------------------------------
2                      | Phone Number           |  4
------------------------------------------------------------------
3                      | City                   |  4
-------------------------------------------------------------------

Table 3 : User_Profile_attribute


User_profile_id        | Profile_attribute_id   |  profile_attribute_value 
--------------------------------------------------------------------------

1                      | 1                      |  23,Times road
--------------------------------------------------------------------------
1                      | 2                      |  9786530874
--------------------------------------------------------------------------
1                      | 3                      |  New York
--------------------------------------------------------------------------

Needed Final Result :

-------------------------------------------------------------------------------------------
User_profile_id|Profile_form_id|Email |Zipcode|Firstname|Lastname|Address|PhoneNumber|City
-------------------------------------------------------------------------------------------

1              |4              |gm.com|123456 |john     |peter   |addr1  |9786530874 |NewY 
-------------------------------------------------------------------------------------------

How to i build the mysql query for the above result.

  • 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-11T07:23:21+00:00Added an answer on June 11, 2026 at 7:23 am

    You are need to do is JOIN the tables and then PIVOT the data. However MySQL does not have a PIVOT function but you can replicate the functionality using an aggregate function and a CASE statement.

    If you know the values to turn into columns then you can use the following:

    select up.user_profile_id,
      up.profile_form_id,
      up.email,
      up.zipcode,
      up.firstname,
      up.lastname,
      min(case when paf.Profile_attribute_name= 'Address' 
                then upa.profile_attribute_value end) as Address,
      min(case when paf.Profile_attribute_name= 'Phone Number' 
                then upa.profile_attribute_value end) as PhoneNumber,
      min(case when paf.Profile_attribute_name= 'City' 
                then upa.profile_attribute_value end) as City
    from user_profile up
    left join Profile_attribute_form paf
      on up.Profile_form_id = paf.Profile_form_id
    left join User_Profile_attribute upa
      on paf.profile_attribute_id = upa.Profile_attribute_id
      and up.User_profile_id = upa.User_profile_id
    group by up.user_profile_id,
          up.profile_form_id,
          up.email,
          up.zipcode,
          up.firstname,
          up.lastname
    

    see SQL Fiddle with Demo

    Now if you want to perform this dynamically, meaning you do not know ahead of time the columns to transpose, then you should review the following article:

    Dynamic pivot tables (transform rows to columns)

    Your code would look like this:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'min(case when paf.Profile_attribute_name = ''',
          paf.Profile_attribute_name,
          ''' then upa.profile_attribute_value end)  AS ',
          replace(paf.Profile_attribute_name, ' ', '')
        )
      ) INTO @sql
    from user_profile up
    left join Profile_attribute_form paf
      on up.Profile_form_id = paf.Profile_form_id;
    
    SET @sql 
      = CONCAT('select up.user_profile_id,
                  up.profile_form_id,
                  up.email,
                  up.zipcode,
                  up.firstname,
                  up.lastname, ', @sql, '
               from user_profile up
               left join Profile_attribute_form paf
                 on up.Profile_form_id = paf.Profile_form_id
               left join User_Profile_attribute upa
                 on paf.profile_attribute_id = upa.Profile_attribute_id
                 and up.User_profile_id = upa.User_profile_id
               group by up.user_profile_id,
                     up.profile_form_id,
                     up.email,
                     up.zipcode,
                     up.firstname,
                     up.lastname');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    see SQL Fiddle with Demo

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

Sidebar

Related Questions

I have a social app that has a user_profile table with typical standard fields,
I have to tables. One of them, user_profile , has a field named user_profile_id
I have a table that has account numbers in (account_num) and user profiles (profile_id).
Just using this as an example... Here are the columns in my UserProfile table:
table user: |id|name|employee_priority_id|user_priority_id| table priority: |id|name| As you can see, there are two foreign
I have a table user and in addition INNER JOIN it with table cities
I have a table: **user** id, name, points Can you suggest me how to
I have a table (user) that contains user information. I have another table (userview)
I Using Two Table User And Location In User Table I having LocationId And
I have this table: User id INT PK login VARCHAR UNIQUE I want to

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.