Is there any way to retrieve data like this (first row is column header):
data no answer
data1 1 X1
data1 2 X2
data1 3 X3
data1 5 X5
data2 1 Y1
data2 2 Y2
data2 4 Y4
in format like this (again, first row is column header):
data no1 no2 no3 no4 no5
data1 X1 X2 X3 null X5
data2 Y1 Y2 null Y4 null
using SQL query (in MySQL)?
Unfortunately, MySQL does not have PIVOT functionality. So you will have to perform this another way.
Using
CASEstatements:See SQL Fiddle with Demo