我有两个表命名Districts
和Schools
。 该Districts
表格包含一个名为列Schools
。
我需要填充Schools
的列Districts
从对应表Schools
表,这样在每一行Districts
表有一个逗号分隔的从学校名称的值列表Schools
表。
我怎样才能做到这一点? 我应该使用UPDATE
查询或存储过程?
我只得到尽可能:
SQL小提琴
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 |
+------------+------+---------+