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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:50:01+00:00 2026-06-10T15:50:01+00:00

I have read most of the posts on stackoverflow on how to do a

  • 0

I have read most of the posts on stackoverflow on how to do a pivot table but all of the posts show examples with prior knowledge of the columns. How do you construct a query if you have no knowledge of what the columns will be. here is some sample data:

id       column       value       Row
1        age          13          1
2        height       55          1
3        fav number   NULL        1
4        siblings     4           1
5        age          55          2
6        height       54          2
7        fav number   12          2

I am looking for this output:

row        age       height        fav number       siblings
1          13        55            NULL             4
2          55        54            12               NULL

As you can see there is no row 2 is missing an entry for siblings. The column names are unknown at the time of the query. How would you make this query.

  • 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-10T15:50:03+00:00Added an answer on June 10, 2026 at 3:50 pm

    I don’t see any way you can just write some fancy SELECT query to get what you want. You’re going to have to do some pre-processing.


    You have to be executing this MySQL query from some sort of program, application, script, etc. Not sure what the language is, but here’s what I would do in PHP:

    /* $data is where our data is going to be stored in our desired format */
    $data = array();
    /* $columns is a list of all column names */
    $columns = array();
    /* $rows is a list of all row names (probably '1', '2', etc) */
    $rows = array();
    
    $result = mysql_query('SELECT column, value, row FROM TableName');
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      /* if this row isn't in $data yet, add it */
      if (!array_key_exists($row['row'], $data) {
        $data[$row['row']] = array();
      }
    
      /* if this column isn't in $columns yet, add it */
      if (!in_array($row['column'], $columns)) {
        array_push($columns, $row['column']);
      }
    
      /* if this row isn't in $rows yet, add it */
      if (!in_array($row['row'], $rows)) {
        array_push($rows, $row['row']);
      }
    
      /* set the actual value in our multi-dimensional array $data */
      $data[$row['row']][$row['column']] = $row['value'];
    }
    /* free the result (php specific thing) */
    mysql_free_result($result);
    
    /* if we didn't set anything (row, column) pairs, set it to null in $data */
    foreach ($rows as $r) {
      foreach ($columns as $c) {
        if (!array_key_exists($c, $data[$r])) {
          $data[$r][$c] = null;
        }
      }
    }
    

    This will put all the data into a format you want in an array in PHP.


    For example, after running this algorithm on the sample data you provided above, you would be able to do:

    echo $data['2']['age']; // $data['row']['column']
    

    Which would output 55.


    OR if your database isn’t being updated in real time (you have a bunch of data that you’d like to reformat once, rather than continuously), you could extend the script above to also have some “CREATE TABLE”, “INSERT INTO” queries that basically recreate the table in the format you’re looking for.

    Furthermore, if you ARE receiving data in realtime, you can still write the script described above, but you’d just want to remove the rows from the original table as you processed them, and then just run the script whenever data is being put into the original table.

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

Sidebar

Related Questions

I have read most of the WCF REST 404 posts but none that helped
I have read various posts in stackoverflow and I am trying to figure out
I have read few articles about table partioning but still I am bit confused
I have read most of the posts on here regarding floating point, and I
I have read through related posts but have not yet found an answer so
I have read a couple of posts on this topic, but neither addresses my
I've read most of the posts regarding this matter but I find nothing that
I have read many posts on this topic; among them and most recently .NET
I have read countless blogs, posts and StackOverflow questions about the new features of
I have read several articles and several stackoverflow.com posts about expression tree. It is

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.