I have a table of product parts like this:
Parts
part_id part_type product_id
--------------------------------------
1 A 1
2 B 1
3 A 2
4 B 2
5 A 3
6 B 3
and I want a query that will return a table like this:
product_id part_A_id part_B_id
----------------------------------------
1 1 2
2 3 4
3 5 6
In its actual implementation there will be millions of product parts
SQL Server has a PIVOT keyword, but with MySQL you will need to use either a lot of CASE/IF statements or a lot of JOINs.
Here is a previous post of how to do this.
Unfortunately, MySQL does not have a
PIVOT
function but you can model it using an aggregate function and aCASE
statement. For a dynamic version, you will need to use prepared statements:See SQL Fiddle With Demo
If you had only a few columns, then you can use a Static version:
just reference it twice - (you don't really have much of a question here)