我在MySQL中有一个像这样的表:
+---------+---------+---------+---------+
| teacher | grade_1 | grade_2 | grade_3 |
+---------+---------+---------+---------+
| Paul | 1 | 0 | 3 |
| Mike | 2 | 2 | 1 |
| Jens | 3 | NULL | 2 |
| Jens | 2 | 3 | NULL |
| Mike | 1 | 2 | 1 |
| Paul | 2 | 2 | 2 |
| Mike | 1 | NULL | NULL |
+---------+---------+---------+---------+
我想产生这样的结果:
+---------+-------+-------+
| teacher | grade | count |
+---------+-------+-------+
| Jens | NULL | 2 |
| Jens | 0 | 0 |
| Jens | 1 | 0 |
| Jens | 2 | 2 |
| Jens | 3 | 2 |
| Mike | NULL | 2 |
| Mike | 0 | 0 |
| Mike | 1 | 4 |
| Mike | 2 | 3 |
| Mike | 3 | 0 |
| Paul | NULL | 0 |
| Paul | 0 | 1 |
| Paul | 1 | 1 |
| Paul | 2 | 3 |
| Paul | 3 | 1 |
+---------+-------+-------+
因此,每个年级都可能有四个不同的不同值; NULL,0,1,2或3 对于每位教师,我想计算每个年级栏目中每个年级的出现次数。
这可能吗?实际的表格更大,有更多的等级栏目和更多的教师。
我的大脑完全停滞不前,我似乎无法想办法做到这一点......
祝你好运, 延