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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:11:30+00:00 2026-06-13T06:11:30+00:00

I have one table that looks like this called survey_1: ================================================ |id | token

  • 0

I have one table that looks like this called survey_1:

================================================
|id  | token  |     1X2X1     |    1X2X2       |
=====+========+===============+================|
| 1  |   1    |    YES        | Justin Beiber  |
|----+--------+---------------+----------------|
| 2  |   1    |    YES        | Britney Spears |
|----+--------+---------------+----------------|

note: 1X2X1 represents- survey-id X group-id X question-id

I have another table called survey_questions:

===============================================================
|sid |   gid  |   qid  |             question                 |
=====+========+===============+===============================|
| 1  |  2     |    1   |  Do you listen to music?             |
|----+--------+-----------------------------------------------|
| 1  |  2     |    2   |  Who is your favorite music artists? |
|----+--------+-----------------------------------------------|

The sid (survey-id), gid (group-id) and qid(question-id) define that specific question in this table

I need a query that will give me a result like this:

======================================================
|           Question                  |   Answer     |
=========+===========+===============================|
| Do you listen to music?             |    YES       |            
|----------------------------------------------------|
| Who is your favorite music artists? | Justin Beiber| 
|----------------------------------------------------|

NOTE: My database contains thousands of these columns, so it would be very time consuming to edit every survey to match up perfectly in this format.

Can anyone help out with this? Thank you

  • 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-13T06:11:31+00:00Added an answer on June 13, 2026 at 6:11 am

    You need to use ‘UNPIVOT’, which MySQL unfortunately does not support. You can do a similar thing by hardcoding the column names (but you need to know all the columns in advance) like this:

    SELECT survey_questions.Question,
           CASE survey_questions.qid 
             WHEN 1 THEN survey_1.`1X2X1`
             WHEN 2 THEN survey_1.`1X2X2`
             WHEN 3 THEN survey_1.`1X2X3`
             WHEN 4 THEN survey_1.`1X2X4`
           // ...
           END as Answer
    FROM survey_questions
    JOIN survey_1 
      ON survey_questions.qid = survey_1.id
      AND survey_questions.gid = survey_1.token_id
    WHERE survey_questions.sid = 1
    

    Of course, you can always use some scripting language to generate the column names for you… For example, here is a stored procedure you can make:

    CREATE PROCEDURE 'get_qa_for_survey'
    (
      IN surveyId INT
    )
    BEGIN
      DECLARE query1 TEXT; 
      SET @tableName = 'survey_' + surveyId;
      SET query1 = 'SELECT survey_questions.Question,
           CASE survey_questions.qid ';
    
      DECLARE col_names CURSOR FOR
        SELECT column_name
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE table_name = @tableName
        AND (column_name LIKE surveyId +'X%');
        ORDER BY ordinal_position;
    
      select FOUND_ROWS() into num_rows;
    
      SET i = 1;
      the_loop: LOOP    
         IF i > num_rows THEN
            CLOSE col_names;
            LEAVE the_loop;
         END IF;
    
         FETCH col_names 
         INTO col_name;     
         SET query1 = query1 + ' WHEN ' + i + ' THEN ' + @tableName + '.' + col_name
         SET i = i + 1;  
      END LOOP the_loop;
    
      SET query1 = query1 + ' END as Answer
        FROM survey_questions
        JOIN ' + @tableName + '
          ON survey_questions.qid = ' + @tableName + '.id
          AND survey_questions.gid = ' + @tableName + '.token_id
        WHERE survey_questions.sid = ' + surveyId; 
    
    
      SET @Sql = query1;        
      PREPARE STMT FROM @Sql; 
      EXECUTE STMT; 
      DEALLOCATE PREPARE STMT;
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one sql table that looks like this called posts: id | user
Let's just say I have a table called TABLE_NAME that looks like this: id
I have a table called ratings that looks like this Rating | Picid |
I have an SQL table called "posts" that looks like this: id | category
I have an SQL table called trainings that looks like this: +-----+-----------+--------+------------+------------+------------+-------+ | Id
Lets say I have one table called REVIEWS This table has Reviews that customers
I have a categories table that looks like this: id | name | parent
I have a function that looks like this: function reset_all_lightbox_assets() { $(document).one(click, function ()
So, I have an app/assets/stylesheets/ directory structure that looks something like this: |-dialogs |-mixins
So in javascript I have an array that looks about like this: [{x1:0,x2:2000,y:300},{x1:50,x2:250,y:500}] And

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.