0

I have a sample table like

enter image description here

I want to get records like

Array(
  'col-1' => ['v1','v4','v7'],
  'col-2' => ['v2','v5','v8'],
  'col-3' => ['v3','v6','v9'],
)

Is there a way to get it in MySQL

Mudassar Khani
  • 1,469
  • 1
  • 20
  • 39

3 Answers3

1

Please check if this thread helps you.
How to transform vertical data into horizontal data with SQL?

Manoj-kr
  • 776
  • 5
  • 18
1

From the way you listed your desired output I am guessing that you are using PHP. Just in case you are interested in a much simpler PHP based solution to your problem, here it is:

$r=mysqli_query($connection,'select * from tbl');
$arr=array(); $res=array();
while ($d=mysqli_fetch_assoc($r)) $arr[]=$d;
foreach ($arr as $d)
  foreach ($d as $key => $val) $res[$key][]=$val;

print_r($res);
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
1

Hi I think you need to change your DB structure

you can create two tables e.g

// Product Table i wish
id | name
---------
1  | test
2  | Examplace product

then you can create the other table

product_colum_table

id | product_id | product_colum_or_data
1  | 1          |  Something
2  | 1          | some other data 
3  | 2          | product name
4  | 2          | test

then you can have access the using join.

I hope this helps

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66