I need to pivot a table, it can either be done for a view or dumping data into a pre-existing table already setup as a pivoted table. My main question is knowing how to SELECT and pivot the data.
Sample data
Table 1:
user_id user_email first_name last_name
-------------------------------------------------------------
1 jdoe@huh.com John Doe
2 jcarter@aol.com Jimmy Carter
Table 2:
user_id type job_title job_description
---------------------------------------------------------
1 Job 1 Some Job play all day!
1 Job 2 Another Job all work no play!
2 Job 1 Clown IT's not funny
PIVOTED TABLE
what i'm trying to achieve is:
I've shortened the table names, and data for ease of use.
uid email fname lname jtitle-1 jdesc-1 jtitle-2 jdesc-2
----------------------------------------------------------------------------
1 jdoe@> John Doe Some Job play all> Another J> all wo>
2 jcar@> Jimmy Carter Clown IT's not> null null
I've looked around for some examples, but none are exactly what i'm looking for.
MySQL pivot table
http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/
UPDATE
Naturally, soon after posting this i came across a solution.
http://stratosprovatopoulos.com/web-development/mysql/pivot-table-with-dynamic-columns/
This does the trick
MAX(IF(pa.fieldname = ‘size’, pa.fieldvalue, NULL)) AS size