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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:41:07+00:00 2026-06-05T15:41:07+00:00

I have troubles with the following database design (simplified for the example). It allows

  • 0

I have troubles with the following database design (simplified for the example). It allows people to create custom forms, quite similar to google spreadsheat forms. The form generation script is done, including saving the answers to the database. I have trouble displaying the user input.

Table forms (each form is a row in this table)

form_id | form_name
1         Contact Form
2         BBQ sign up Form

Table questions (questions in the form)

question_id | form_id | question_name      | sorting_order
1             1         Full Name            1
2             1         E-mail address       2
3             1         Subject              3
4             1         Message              4
5             2         Your name            1
6             2         Are you vegetarian?  2
7             2         Beer or wine?        3    

table completed_forms (to group answers together)

complete_id | form_id | timestamp
1            1          1339496914
2            1          1148691493
3            2          1256235334

table answers (answers from users to specific questions)

answer_id | complete_id | question_id | answer
1           1             1             Barack Obama
2           1             2             president@whitehouse.gov
3           1             3             Test message
4           1             4             This is a test. Regards, Barack.
5           2             1             Thomas something
6           2             2             thomas@email.com
7           2             3             Another message
8           2             4             Hey, it's Thomas. This is my message.
9           3             5             Dirk
10          3             6             No, I love meat
11          3             7             Red wine!

What I want to display is an overview of user input, which is quite complex as the names and number of columns differ per form. For example, I want to show the user input from the contact form (form_id: 1) as follows:

id | timestamp | Full Name | E-mail Address | Subject | Message
1    133949...   Barack...   president@w...   Test...   This is a t...
2    114869...   Thomas...   thomas@emai...   Anot...   Hey, it's T...

And from form_id 2 as follows:

id | timestamp | Your name | Are you vegetarian? | Beer or Wine?
3    125623...   Dirk        No, I love meat       Red wine!

Does anyone if and how I can achieve this?

Many thanks for anyone who is willing to take the time to shine a light on this problem!

  • 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-05T15:41:09+00:00Added an answer on June 5, 2026 at 3:41 pm

    Basically you are trying to write a PIVOT but MySQL does not have a PIVOT function per se, so you will have to write the query a little different. For a static version of this query where you will code in the values of the questions you can use the following:

    Form 1 (See SQL Fiddle with Demo):

    select cf.complete_id
      , cf.ts
      , Min(CASE WHEN q.question_name = 'Full Name' THEN a.answer END) as 'Full Name'
      , Min(CASE WHEN q.question_name = 'Email Address' THEN a.answer END) as 'Email Address'
      , Min(CASE WHEN q.question_name = 'Subject' THEN a.answer END) as 'Subject'
      , Min(CASE WHEN q.question_name = 'Message' THEN a.answer END) as 'Message'
    from completed_forms cf
    inner join questions q
      on cf.form_id = q.form_id
    inner join answers a
      on cf.complete_id = a.complete_id
      and q.question_id = a.question_id
    where cf.form_id = 1
    group by cf.complete_id
    

    Form 2 (See SQL Fiddle with Demo):

    select cf.complete_id
      , cf.ts
      , Min(CASE WHEN q.question_name = 'Your Name' THEN a.answer END) as 'Your Name'
      , Min(CASE WHEN q.question_name = 'Are you vegetarian?' THEN a.answer END) as 'Are you vegetarian?'
      , Min(CASE WHEN q.question_name = 'Beer or Wine' THEN a.answer END) as 'Beer or Wine'
    from completed_forms cf
    inner join questions q
      on cf.form_id = q.form_id
    inner join answers a
      on cf.complete_id = a.complete_id
      and q.question_id = a.question_id
    where cf.form_id = 2
    group by cf.complete_id
    

    Now since your questions are going to change for each form, then you should look at automating the Pivot. I suggest reviewing the article located here:

    http://www.artfulsoftware.com/infotree/queries.php#523

    It contains examples of how to use a pivot in MySql. There are also other answers on StackOverflow that should be able to help with writing this dynamically:

    mysql query to dynamically convert row data to columns

    MySQL pivot table with dynamic headers based on single column data

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

Sidebar

Related Questions

I have the following data being returned from my database query: +---------------+-----------+------------------+--------------+-------+ | district_name
I have some trouble with this. I have one database with the following tables:
servers all sql server 2008, and win xp i have the following task create
Using an MS Access database with the following design: Tables Equipment, Employees, CreditCard Fields
Following is the simple database model i have: class Notes(db.Model): text = db.StringProperty(multiline=True) date
I have trouble finding a solution for the following problem: I have a lot
I have trouble to get gluLookAt working. I have the following code which works
I am having trouble with the following SQL statement. I have had this issue
I have troubles using a foreign protocol in Objective-C. Is there any other solution
I have troubles animating a StackPanel with the VisualStateManager . VisualStateManager.GoToState() accepts a Control

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.