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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:33:25+00:00 2026-06-17T02:33:25+00:00

I have the schema like the following: CREATE TABLE prop_set ( id INT UNSIGNED

  • 0

I have the schema like the following:

CREATE TABLE prop_set (
      id INT UNSIGNED NOT NULL AUTO_INCREMENT,
      name varchar(255),
      PRIMARY KEY (id)
  );
CREATE TABLE users_props (
      user_id int UNSIGNED NOT NULL,
      prop_id int UNSIGNED NOT NULL,
      value text, 
      PRIMARY KEY (user_id,prop_id)
  );
CREATE TABLE user (
      id INT UNSIGNED NOT NULL AUTO_INCREMENT,
      name varchar(255),
      PRIMARY KEY (id)
  );

INSERT INTO prop_set SET name="prop1";
INSERT INTO prop_set SET name="prop2";
INSERT INTO prop_set SET name="prop3";


INSERT INTO user SET name="user1";
INSERT INTO user SET name="user2";

INSERT INTO users_props set user_id=1,prop_id=1,value="prop1 user1";
INSERT INTO users_props set user_id=1,prop_id=2,value="prop2 user1";
INSERT INTO users_props set user_id=1,prop_id=3,value="prop3 user1";

INSERT INTO users_props set user_id=2,prop_id=1,value="prop1 user2";
INSERT INTO users_props set user_id=2,prop_id=2,value="prop2 user2";
INSERT INTO users_props set user_id=2,prop_id=3,value="prop3 user2";

now I run the select like following:

SELECT u.name,ps.name AS prop,up.value
      FROM USER u
        JOIN users_props up ON u.id=up.user_id
        JOIN prop_set ps ON ps.id=up.prop_id;

and get output:

|  NAME |  PROP |       VALUE |
-------------------------------
| user1 | prop1 | prop1 user1 |
| user2 | prop1 | prop1 user2 |
| user1 | prop2 | prop2 user1 |
| user2 | prop2 | prop2 user2 |
| user1 | prop3 | prop3 user1 |
| user2 | prop3 | prop3 user2 |

fiddle
Is there any way to make mysql to return the following output ?

| NAME  |   prop1     |     prop2   |    prop3    |
---------------------------------------------------
| user1 | prop1 user1 | prop2 user1 | prop3 user1 |
| user2 | prop1 user2 | prop2 user2 | prop3 user2 |

I looking for mysql – only solution.

  • 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-17T02:33:26+00:00Added an answer on June 17, 2026 at 2:33 am

    The pivot syntax of MySQL uses an aggregate function with a CASE statement similar to this:

    SELECT u.name,
      max(case when ps.name = 'prop1' then up.value else null end) Prop1,
      max(case when ps.name = 'prop2' then up.value else null end) Prop2,
      max(case when ps.name = 'prop3' then up.value else null end) Prop3
    FROM user u 
    JOIN users_props up 
      ON u.id=up.user_id 
    JOIN prop_set ps 
      on ps.id=up.prop_id
    GROUP BY u.name;
    

    See SQL Fiddle with Demo

    The above works great if you have known values to transpose into columns, but if the values are unknown, then you will want to look at using a prepared statement to generate dynamic SQL:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'max(case when ps.name = ''',
          name,
          ''' then up.value else null end) AS ''',
          name, ''''
        )
      ) INTO @sql
    FROM  prop_set;
    
    SET @sql = CONCAT('SELECT u.name, ', @sql, ' 
                      FROM user u 
                      JOIN users_props up 
                        ON u.id=up.user_id 
                      JOIN prop_set ps 
                        on ps.id=up.prop_id
                      GROUP BY u.name');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See SQL Fiddle with Demo

    The result is same with both versions:

    |  NAME |       PROP1 |       PROP2 |       PROP3 |
    ---------------------------------------------------
    | user1 | prop1 user1 | prop2 user1 | prop3 user1 |
    | user2 | prop1 user2 | prop2 user2 | prop3 user2 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have schema similar to the following: create table bar ( instrument varchar(255) not
I have following table schema - CREATE TABLE [dbo].[TEST_TABLE] ( [TEST_TABLE_ID] [int] IDENTITY(1,1) NOT
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
I have a table which have schema like this id name 1 jack 2
I have an orders table with a schema like this. CREATE TABLE orders (
I have a schema that essentially looks like this: CREATE TABLE `data` ( `id`
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
Schema: CREATE TABLE [Groups] ([Group] VARCHAR(50) NULL, [ArtNum] INTEGER NULL, [ID] VARCHAR(50) NULL, [Time]
I have a following SQL schema layout: -- postgresql82+ syntax create table AudioTracks (
I have the following schema, which I've simplified slightly: CREATE TABLE [dbo].[Header] ( [HeaderId]

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.