I’m trying to create a nested structure;
id | degerAdi | deger
_____________________________
1 | asd | 1
2 | asd | 2
3 | asd | 345
4 | rty | 6765
5 | rty | ljkl
6 | hhh | 567
7 | hjh | 5674
8 | ffgu | 567
9 | qwe | 345345
10 | qwe | fghfghfh
11 | qwe | ghghjghjgj
I need a way to get result like the values of ‘degerAdi’ colmn as colmn names and associated values of ‘deger’ colmn as values (rows) of these colms.
(Sorry for my poor english language skills.)
I’need result like this;
asd | rty | hhh | hjh | ffgu | qwe
-----------------------------------------------------
1 | 6765 | 567 | 5674 | 567 | 345345
2 | 6765 | NULL | NULL | NULL | fghfghfh
345 | NULL | NULL | NULL | NULL | ghghjghjgj
You are trying to
PIVOTthe data but MySQL does not have aPIVOTfunction. Also to make this easier, you will want to partition the data based on thedegerAdivalue to apply a rownumber. If you have a known number of columns, then you can use:See SQL Fiddle With Demo
If you have an unknown number of columns then you will want to use prepared statements:
See SQL Fiddle with demo