需要从两个mysql表创建动态矩阵

nfs0ujit  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(205)

我有两个mysql表,它们有以下模式。
学生:

+------------+--------------+
| student_id | student_name |
+------------+--------------+
| 1          | John Doe     |
+------------+--------------+
| 2          | Robert Brown |
+------------+--------------+
| 3          | Charlie Cox  |
+------------+--------------+

学科:

+------------+--------------+
| student_id | subject_name |
+------------+--------------+
| 1          | History      |
+------------+--------------+
| 1          | Biology      |
+------------+--------------+
| 1          | Mathematics  |
+------------+--------------+
| 2          | Literature   |
+------------+--------------+
| 2          | Physics      |
+------------+--------------+
| 2          | Mathematics  |
+------------+--------------+
| 3          | Literature   |
+------------+--------------+
| 3          | Geography    |
+------------+--------------+

现在我想要一个结果矩阵below:-

+--------------+---------+-----------+---------+------------+-------------+---------+
| student_name | Biology | Geography | History | Literature | Mathematics | Physics |
+--------------+---------+-----------+---------+------------+-------------+---------+
| John Doe     | Y       | N         | Y       | N          | Y           | N       |
+--------------+---------+-----------+---------+------------+-------------+---------+
| Robert Brown | N       | N         | N       | Y          | Y           | Y       |
+--------------+---------+-----------+---------+------------+-------------+---------+
| Charlie Cox  | N       | Y         | N       | Y          | N           | N       |
+--------------+---------+-----------+---------+------------+-------------+---------+

随着更多的主题添加到数据库中,包含主题的列可能会增长得更多。根据我的要求,有一些关于pivot的建议,但我对此并不熟悉。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题