I've got 2 tables where i want to join the header and the first column within an prepared statement. I've got managed to join the column, but how to join the header?
Table1 :
ID |Name
----------
1 | A
2 | B
3 | C
4 | D
5 | E
Table2 :
TeamA|TeamB|Won|Lost
--------------------
1 | 2 | 5 | 3
1 | 3 | 2 | 4
1 | 4 | 9 | 1
2 | 5 | 5 | 5
3 | 1 | 2 | 4
Result-Matrix:
| A | B | C | D | E
----------------------------
A | 0 | 2 | -2 | 8 | 0
B | 0 | 0 | 0 | 0 | 0
C | -2 | 0 | 0 | 0 | 0
In order to get the team names from the ids, you will have to join on the table twice.
If you know all the values, the static version of the code is:
See SQL Fiddle with Demo.
Then if you are using a prepared statement to create this dynamically, the code will be:
See SQL Fiddle with Demo
The result is: