0

I have a MySQL table with the following data. I want to display the corresponding MySQL query result in columns. Its is questions and answers survey. There are 47 questions in a survey. SO when a user submit a survey, it will save all the 47 answers with their corresponding questionId in the table. So instead of having 47 rows of answers, I want to have it in 1 row with 47 columns. Please click on this link to see MySQL table records as example

mysql table data

Now what I want to achieve is

Q1     | Q2     | Q3    | Q4     |--- |Q47
-----------------------------------------------------
Answer | answer |answer |answer  |answer
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
codeDev
  • 1
  • 1
  • 2
    What the hell?? That's DBA's worst nightmare. – Phiter Feb 21 '16 at 18:54
  • 1
    No. 47 rows. Transpose ("pivot") in your app if necessary. – Rick James Feb 21 '16 at 19:06
  • try this: http://stackoverflow.com/questions/7674786/mysql-pivot-table – MaxU - stand with Ukraine Feb 21 '16 at 19:07
  • Do you use MySQL to compose ring tones? No? Why not? – Strawberry Feb 21 '16 at 22:30
  • This is what i have done so far. It works but it groups my data. I don't want my data grouped. SELECT agentId, MAX(IF(questionid= 1, answer, '')) Q1, MAX(IF(questionid= 2, answer, '')) Q2, MAX(IF(questionid= 3, answer, '')) Q3, MAX(IF(questionid= 4, answer, '')) Q4, MAX(IF(questionid= 5, answer, '')) Q5, .... MAX(IF(questionid= 51, answer, '')) Q51 FROM surveyanswers GROUP BY agentId ORDER BY agentId; – codeDev Feb 22 '16 at 13:58

0 Answers0