If MySQL table looking something like this
And what I'm trying to do is a pivot table looks like this:
Jenny Ashley Meera Jane
Samantha Christeen Priya Julia
NULL Ketty NULL Maria
each column based on their occupation
If MySQL table looking something like this
And what I'm trying to do is a pivot table looks like this:
Jenny Ashley Meera Jane
Samantha Christeen Priya Julia
NULL Ketty NULL Maria
each column based on their occupation
This code will do something near what you want:
set @sql = (
select group_concat(distinct
concat(
"(case when `Occupation`='", Occupation, "' then `Name` end) as `", `Occupation`, "`"
)
)
from t
);
set @sql = concat("select ", @sql, " from t ");
prepare stmt from @sql;
execute stmt;
There are plenty of ways of achieving this described here: MySQL pivot table
where the dynamic way of doing it is included in the answer by Abhishek Gupta using GROUP_CONCAT with CONCAT. It is not fast but it is dynamic.
The gist of it is that if you want a dynamic solution then you first have to do a query to find out what columns there are in your data. Then you create a new query based in that information.
If you want a dynamic solution that is also fast then you need to pull the first query into your programming language (like php or whatever you are using), and use the programming language to create a query that is fast.