I have 3 tables as follows:
tbl_reporting tbl_criteria tbl_student
|===============================| |========================| |===============|
| id | id_criteria | id_student | | id | name |weight | | id | name |
|===============================| |========================| |===============|
| 1 | 2 | 3 | | 1 | worrying | 3 | | 1 | Nina |
| 2 | 1 | 2 | | 2 | naughty | 2 | | 2 | Adam |
| 3 | 1 | 1 | | 3 | usually | 2 | | 3 | Dodi |
| 4 | 2 | 2 | | 4 | good | 1 | | 4 | Zarah |
| 5 | 1 | 1 | | 5 | obey | 1 | | 5 | Udep |
the actual result I want are as like follow, with count the weight on tbl_criteria
result
| student | worrying | naughty | usualy | good | obey | total |
|=============================================================|
| Nina | 6 | 0 | 0 | 0 | 0 | 6 |
| Adam | 3 | 2 | 0 | 0 | 0 | 5 |
| Dodi | 0 | 2 | 0 | 0 | 0 | 2 |
| Zarah | 0 | 0 | 0 | 0 | 0 | 0 |
| Udep | 0 | 0 | 0 | 0 | 0 | 0 |
So, all values in tbl_criteria.name
field will become with dynamic a new column , and count the weight.
I created a table and its inputs: http://sqlfiddle.com/#!9/41e637
v10.3.16-MariaDB