Skip to content
Advertisement

mysql how do i convert row results to column with new column name?

I have 2 mysql tables that i want to join, but i want the column ‘category’ results to be in a new column, not rows.

the first one looks something like this

and the second one looks like this

and i have another table to store the relations that looks like this

how can i output a table where the result looks like this

I read about pivot but i can’t wrap my head around it (really new to mysql and coding in general to be honest). Thank you!

Advertisement

Answer

In MySQL 8.0, you can address this with joins, row_number() to enumerate the categories per product, and conditional aggregation to pivot the resultset:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement