我正在尝试从三个表中生成一个透视视图:
- 学生们
- 费用
- stud_fee(关系表)
表格:
Students Table
+----+-----------+-----------+----------------+----------------+-------+
| id | school_id | last_name | first_name | middle_initial | yrlvl |
+----+-----------+-----------+----------------+----------------+-------+
| 1 | 2080295 | Doe | John | A | 3 |
| 2 | 0239129 | Rizal | Jose | M | 4 |
| 3 | 1231238 | Santos | Jane | M | 2 |
+----+-----------+-----------+----------------+----------------+-------+
Fee table
+----+--------------------+------------+
| id | fee_name | fee_amount |
+----+--------------------+------------+
| 1 | Registration Fee | 100 |
| 2 | News Letter | 100 |
| 3 | T-Shirt | 250 |
| 4 | Party | 500 |
+----+--------------------+------------+
stud_fee table
+----+------------+-----+
| id | stud_id | fee_id |
+----+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 1 |
| 5 | 3 | 1 |
| 6 | 3 | 4 |
+----+---------+--------+
我想将费用作为列,将学生作为行。我想让它显示为:
+-----------+------------------+-------------+---------+-------+-------+
| school_id | Registration Fee | News Letter | T-Shirt | Party | Total |
+-----------+------------------+-------------+---------+-------+-------+
| 2080295 | 100 | 100 | 250 | | 450 |
| 0239129 | 100 | | | | 100 |
| 1231238 | 100 | | | 500 | 600 |
+-----------+------------------+-------------+---------+-------+-------+