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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:05:13+00:00 2026-06-07T18:05:13+00:00

I have a database like this: id User_id Question_id Answer 1 john Question_1 b

  • 0

I have a database like this:

id        User_id    Question_id     Answer
1           john      Question_1        b
2           john      Question_2        a
3           john      Question_3        d
4           harry     Question_1        a
5           harry     Question_2        a  
6           harry     Question_3        c 
7           colleen   Question_1        c
8           colleen   Question_2        a
9           colleen   Question_3        b 

I want to display the content of the above data in the following format-

id     User_id      Question_1      Question_2        Question_3       
1       john             b               a                 d
2       harry            a               a                 c
4       colleen          c               a                 b

How can I achieve this using sql? I am using mysql and php.

  • 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-07T18:05:15+00:00Added an answer on June 7, 2026 at 6:05 pm
    SELECT
      id,
      User_id,
      MAX(IF(querstion_id = 'Question_1', answer, NULL)) AS Question_1,
      MAX(IF(querstion_id = 'Question_2', answer, NULL)) AS Question_2,
      MAX(IF(querstion_id = 'Question_3', answer, NULL)) AS Question_3
    FROM answers
    GROUP BY User_id
    

    EDIT 1
    added a php version, see comment

    $table = array();
    $query = "SELECT * FROM answers ORDER BY User_id, Question_id";
    
    ... fetch data depending on what interface you use ...
    
    foreach/while(....)
    {
       if(!isset($table[$result['User_id']]))
       {
           $table[$result['User_id']] = array();
           $table[$result['User_id']]['id'] = $result['id'];
           $table[$result['User_id']]['User_id'] = $result['User_id'];
       }
    
       $table[$result['User_id']][$result['Question_id']] = $result['Answer'];
    }
    

    Edit 2 how to display it:

    then just display it as you would done with a normal query,
    this is how i usaly converts php arrays to html:

    echo '<table><thead>';
    echo '<tr><th>' . implode('</th><th>', array_keys(current($table))) . '</th></tr>';
    echo '</thead><tbody>';
    foreach($table as $row)
    {
        echo '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
    }
    echo '</tbody></table>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in my database like this: [id] [uniqueidentifier] NOT NULL, [user_id]
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have mysql database like this id | code 1 | bok-1 2 |
Suppose I have a database like this: This is set up to give role-wise
Let's say i have a database design like this: for the data: id |
I have a database that look like this: [Invoice] -PK:ID -... [InvoiceDetail] -PK:ID -FK:Invoice_ID
I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
I have a database connection that I got like this: db = Mongo::Connection.new.db(app-development) but
I have a mysql database that looks like this: |id | city | created
I have a database application in which a group is modeled like this: TABLE

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.