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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:25:50+00:00 2026-05-26T04:25:50+00:00

Okay, so I know I might get reamed for even suggesting such a thing,

  • 0

Okay, so I know I might get reamed for even suggesting such a thing, but it has me curious…

Let’s suppose I have a table like:

content
---------------
| id | status |

and a table like this:

attributes
---------------------------
| id | pid | name | value |

where some attributes might be

| 1 | 1 | status | published |
| 2 | 1 | viable | yes       |

Is is possible to do query so that the value in the NAME column of attributes would be represented as a column name?

Example:

SELECT a.name AS {a.name value} FROM content c LEFT JOIN attributes a ON a.pid = c.id

So when I run through the results I could do something like

foreach($result as $r){
    echo $r->viable;  //yes
    echo $r->status;  //published
}

I know this doesn’t follow relational databasing, but it would be useful to me for some superfluous attributes. The other option I thought of was storing the data as a serialized string, but that makes me cringe.

  • 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-26T04:25:51+00:00Added an answer on May 26, 2026 at 4:25 am

    MySQL does not have native support for pivot queries, and it cannot pivot an arbitrary number of rows into columns because the columns have to be known ahead of time. In other words, you have to do this with PHP.

    I’m not sure whether multiple objects can be returned from your query or not (each object would hopefully be only one row, but you’ve already admitted to a lack of normalization). If only one is going to be returned each time, it’s even simpler. However, I’m going to assume more than one can be returned and they are keyed on pid:

    $content = array();
    while ($row = $result->fetch()) {
       //Prevent a PHP warning; this step can be skipped
       if (!isset($content[$row->pid])) {
          $content[$row->pid] = array();
       }
    
       $content[$row->pid][$row->name] = $row->value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I know it is weird but when I put this code between <head
Okay, I have done a bit of searching online and found this thread, but
Okay, I think I might be over-complicating this issue but I truly am stuck.
Okay, so the answer to my question might not be the problem but here's
Okay, this might be a very silly beginner question, but: I've got an ClassA,
Okay this is probably a rookie question, but I have never done GUI programming
Okay, I get it. Data in PostgreSQL is case-sensitive. And I know I can
Okay I know this is really stupid.But how to i write SELECT Column1 FROM
Okay the question title may not have made sense... mostly because I don't know
Okay I know this is a really lame question because Starbasic is a really

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.