我是SQL的新手,我想知道是否有一种方法可以连接单元格值。
例如,我想转换下表:
+---------------------+------------------+
| student1 | Mathematics |
| student1 | Science |
| student1 | English |
| student2 | Mathematics |
| student2 | English |
+---------------------+------------------+放入如下所示的表中:
+---------------------+------------------------------------+
| student1 | Mathematics, Science, English |
| student2 | Mathematics, English |
+---------------------+------------------------------------+谢谢
发布于 2020-08-10 21:29:34
你想要group_concat()
select student, group_concat(subject, ', ') as subjects
from t
group by student;在SQL Server中,等效项使用string_agg()
select student, string_agg(subject, ', ') as subjects
from t
group by student;发布于 2020-08-10 21:29:58
使用group_concat()
select col1, group_concat(col2,', ') from tablename
group by col1https://stackoverflow.com/questions/63341216
复制相似问题