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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:21:44+00:00 2026-05-30T21:21:44+00:00

How do I select a column value as a column name and group the

  • 0

How do I select a column value as a column name and group the results as a row.

I have a table as such:

id      articleId   label           value
1       1           title           Example title
2       1           description     This is the description
3       1           author          Me
4       2           title           Example of another type of article
5       2           description     Short description
6       2           author          Someone else

Is it possible to select all of the rows and use the label as the column name and the value as the value of that column name and then group them by the article name.
So how I would like to have it returned:

articleId   title           description     author
1           Example title   This is the..   Me
2           Example of an.. Short descr..   Someone else

I’m using this for a CMS where the user can define the fields for an article so we don’t have to customize the table’s. This is why i’m not making the tables as the I would like to have it returned. I am also aware that I can just as easily convert the result to this in php.

— edit —

Can this be done without knowing what labels are added? In this example im using title, description and author. But it could very well be something totally different like title, shortDescription, availableTo, techInformation, etc.. The idea is that the article’s are customizable for the user without needing to change the database and query’s

  • 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-30T21:21:45+00:00Added an answer on May 30, 2026 at 9:21 pm

    I figured I’d better post as an answer, even if not what OP would like to hear. What you are asking to do is to populate a query with a variable number of columns based on the distinct values within column label, all associated with articleID. Taking your specific example, the following would be the resultant query that I would most likely go to in this instance (though the example from @Devart is equally valid)

    SELECT
        t.id,
        t.articleId,
        t1.value AS title,
        t2.value AS description,
        t3.value AS author
    
    FROM `tableName` t
    LEFT JOIN `tablename` t1
        ON t1.article_id = t.article_id AND t1.label = 'title'
    LEFT JOIN `tablename` t2
        ON t2.article_id = t.article_id AND t2.label = 'description'
    LEFT JOIN `tablename` t3
        ON t3.article_id = t.article_id AND t3.label = 'author'
    

    Now expanding this to account for up to n labels, we get the following query (metacode included, this query will NOT execute verbatim)

    SELECT DISTINCT label FROM `tableName`;
    
    SELECT 
        t.id,
        t.articleId
        // for (i=1;i<= number of distinct labels) {
        ,t[i].value AS [value[i]]
        // }
    
    FROM `tableName` t
    // for (i=1;i<= number of distinct labels) {
    LEFT JOIN `tablename` t[i]
        ON t[i].article_id = t.article_id AND t[i].label = [value[i]]
    // }
    ;
    

    So what you can do is one of the following.

    1. SELECT t.* FROM tablename t and then have PHP process it as required
    2. SELECT DISTINCT label FROM tablename and have PHP build the second query with the many LEFT JOINs (or MAX / GROUP BY logic if preferred)
    3. Create a Stored Procedure to do the same as #2. This would most likely be more efficient than #2 however may be less efficient overall than #1.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a column called menu_order which has no default value. When I select
I have the following query: select column_name, count(column_name) from table group by column_name having
The following two queries each give the same result: SELECT column FROM table GROUP
How to find column's name or header? For example if i select column 5
I have the following query in access 2003 mdb. SELECT Company.Name, Company.Address, Place.Name_of_Place, Sum(Income.Value)
How can I select a specific value in the combobox column in the datagridView
I'm trying to select a column from a single table (no joins) and I
I'm getting the same results from select length(column_name) from table as from select dbms_lob.getlength(column_name)
I have a table x that's like the one bellow: id | name |
I have this query: SELECT p.text,se.name,s.sub_name,SUM((p.volume / (SELECT SUM(p.volume) FROM phrase p WHERE p.volume

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.