Hello i am trying to successfully select rows as columns in mysql.
Maybe someone can help me.
This is what my table looks like
+----------+----------------------------------------------------+----------+
| brand_id | brand_name | linecode |
+----------+----------------------------------------------------+----------+
| DPQT | 1-800 Tow Truck | DER |
| DPQT | 1-800 Tow Truck | DCF |
| DPQT | 1-800 Tow Truck | DCA |
| DPQT | 1-800 Tow Truck | AAA |
| DPQT | 1-800 Tow Truck | DDD |
| BLGR | 1-800-Radiator | Curt |
| BGVM | 100+ Manufacturing/Coyote | ASC |
| DPQS | 10C Technologies Inc | AQW |
| DPQS | 10C Technologies Inc | ASQ |
| FDJG | 2 Cool AirVents | CAS |
and i am trying to get the results to look like :
+----------+----------------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+
| brand_id | brand_name | Code1 | Code2 | Code3 | Code4 | Code5 | Code6 | Code7 | Code8 | Code9 | Code10 |
+----------+----------------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+
| DPQT | 1-800 Tow Truck | DCF | DCA | AAA | DDD | DER | NULL | NULL | NULL | NULL | NULL |
| BLGR | 1-800-Radiator | Curt | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| BGVM | 100+ Manufacturing/Coyote | ASC | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| DPQS | 10C Technologies Inc | ASQ | AQW | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| FDJG | 2 Cool AirVents | CAS | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
As you can see when the brand_id and the brand_name are the same i want to group the line codes into separate columns
The max line codes will ever have is 10. That is why i have Code1-Code10
This is what i am doing right now, and it is not working.
$sql0 = 'set @num := 0';
$result = mysqli_query($dbh,"select brand_id, brand_name,
max(case when group_row_number = 1 then linecode end) Code1,
max(case when group_row_number = 2 then linecode end) Code2,
max(case when group_row_number = 3 then linecode end) Code3,
max(case when group_row_number = 4 then linecode end) Code4,
max(case when group_row_number = 5 then linecode end) Code5,
max(case when group_row_number = 6 then linecode end) Code6,
max(case when group_row_number = 7 then linecode end) Code7,
max(case when group_row_number = 8 then linecode end) Code8,
max(case when group_row_number = 9 then linecode end) Code9,
max(case when group_row_number = 10 then linecode end) Code10
from ( select brand_id, brand_name, linecode, @num := @num + 1 as group_row_number from linecodes_temp ) src
group by brand_id, brand_name
order by if(linecode = '' or linecode is null,1,0), brand_name ASC");
and the results i am getting back are
+----------+----------------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+
| brand_id | brand_name | Code1 | Code2 | Code3 | Code4 | Code5 | Code6 | Code7 | Code8 | Code9 | Code10 |
+----------+----------------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+
| DPQT | 1-800 Tow Truck | DER | DCF | DCA | AAA | DDD | NULL | NULL | NULL | NULL | NULL |
| BLGR | 1-800-Radiator | NULL | NULL | NULL | NULL | NULL | Curt | NULL | NULL | NULL | NULL |
| BGVM | 100+ Manufacturing/Coyote | NULL | NULL | NULL | NULL | NULL | NULL | ASC | NULL | NULL | NULL |
| DPQS | 10C Technologies Inc | NULL | NULL | NULL | NULL | NULL | NULL | NULL | AQW | ASQ | NULL |
| FDJG | 2 Cool AirVents | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | CAS |
I need a way so that once the brand_id and brand_name change the num variable goes back to 0.
As you can see it is doing what i want it to do but once it goes to the next row the num variable continues going up to the next column. So then that is where the next line code gets added. I am assuming i need a if statement in there some where but i don't understand how to do it.
If anyone could update my query so that this happens i would be very thankful
Thank you for your time.