I am trying to turn rows from one table into columns and fill each column with rows from another table.
The answer by Paul Dixon on Mar 16 '09 at 10:23 in this post appears to be very close to what I am trying to achieve. I posted the relavant part of his answer below. How to pivot a MySQL entity-attribute-value schema
SELECT file.*,
attr1.value AS 'Attribute 1 Name',
attr2.value AS 'Attribute 2 Name',
...
FROM
file
LEFT JOIN attr AS attr1
ON(file.FileId=attr1.FileId and attr1.AttributeId=1)
LEFT JOIN attr AS attr2
ON(file.FileId=attr2.FileId and attr2.AttributeId=2)
...
I have created a similar query to the example above:
SELECT
forms.*,
tbl_1.*,
tbl_2.*,
tbl_1.value as FirstName,
tbl_2.value as LastName
FROM t_form_fields as forms
LEFT JOIN t_player_data as tbl_1
ON(forms.id=tbl_1.form_field_id and tbl_1.form_field_id = 2)
LEFT JOIN t_player_data as tbl_2
ON(forms.id=tbl_2.form_field_id and tbl_2.form_field_id = 3)
The current results of running this query are:
form_field_id name player_id value player_id value FirstName LastName
2 First Name 1 Bob NULL NULL Bob NULL
2 First Name 2 Bill NULL NULL Bill NULL
2 First Name 3 Zech NULL NULL Zech NULL
2 First Name 4 Tim NULL NULL Tim NULL
3 Last Name NULL NULL 1 Goodman NULL Goodman
3 Last Name NULL NULL 2 Herbst NULL Herbst
3 Last Name NULL NULL 3 CampbellNULL Campbell
3 Last Name NULL NULL 4 Smith NULL Smith
The desired result is:
player_id FirstName LastName
1 Bob Goodman
2 Bill Herbst
3 Zech Campbell
4 Tim Smith
If you need more information please comment.