我有两个名为Districts
and的表Schools
。该Districts
表包含一个名为 的列Schools
。
我需要从相应Schools
的表格中填充表格的列,以便表格中的每一行都有一个逗号分隔的表格中的学校名称值列表。Districts
Schools
Districts
Schools
我怎样才能做到这一点?我应该使用UPDATE
查询还是存储过程?
我只做到了:
Districts Table
+------------+------+---------+
| DistrictId | Name | Schools |
+------------+------+---------+
| 1 | a | |
| 2 | b | |
| 3 | c | |
| 4 | d | |
+------------+------+---------+
Schools Table
+----------+------------+------------+
| SchoolId | SchoolName | DistrictId |
+----------+------------+------------+
| 1 | s1 | 1 |
| 2 | s2 | 1 |
| 3 | s3 | 2 |
| 4 | s4 | 2 |
| 5 | s5 | 4 |
+----------+------------+------------+
输出需要如何
+------------+------+---------+
| DistrictId | Name | Schools |
+------------+------+---------+
| 1 | a | s1,s2 |
| 2 | b | s3,s4 |
| 3 | c | |
| 4 | d | s5 |
+------------+------+---------+