, SQL-. , , , survey_data_id. survey_data_id. , .
Query
set @query = null;
select
group_concat(distinct
concat(
'max(case when `rn` = ',
`rn`,
' then `favorite` end) as `favorite', `rn` , '`'
)
) into @query
from (
select `survey_id`, `survey_data_id`, `favorite`, (
case `survey_data_id` when @curA
then @curRow := @curRow + 1
else @curRow := 1 and @curA := `survey_data_id` end
) as `rn`
from `project_playlist_indexes` t,
(select @curRow := 0, @curA := '') r
order by `survey_data_id`, `survey_data_id`
) t;
set @query = concat('select t2.`survey_data_id`, t2.`title`,',
@query,
' from (select `survey_id`, `survey_data_id`, `favorite`, (
case `survey_data_id` when @curA
then @curRow := @curRow + 1
else @curRow := 1 and @curA := `survey_data_id` end
) as `rn`
from `project_playlist_indexes` t,
(select @curRow := 0, @curA := '''') r
order by `survey_data_id`, `survey_data_id`) t1
right join `survey_datas` t2
on t1.survey_data_id = t2.`survey_data_id`
group by t1.`survey_data_id`
order by t2.`survey_data_id`;'
);
prepare stmt from @query;
execute stmt;
deallocate prepare stmt;