我有以下數據庫:如何從行中獲取數據並將其放入單個字段?
paperid | authorid | name | affiliation
---------+----------+------------------+---------------------------------------------------------------------------------
487792 | 1487835 | Mark A. Magnuson | Vanderbilt University
487792 | 1814994 | Miriam Magnuson | Department of Education and Psychology, Linköping University, Linköping, Sweden
487792 | 2248713 | Leif Hergils | Department of Audiology, University Hospital, Linköping, Sweden
我想作這樣的事情出來的:
paperid | name |
---------+---------------------------------------------------------+
487792 | Mark A. Magnuson Miriam Magnuson Leif Hergils
通過將所有作者的名字在列/場。
我怎樣才能用SQL來做到這一點?
編輯
我曾嘗試:
select paperid, concat(name) as authors FROM newpaperauthor GROUP BY paperid, name ORDER BY paperid;
但是,這將單獨列出作者:
paperid | authors
---------+----------------------------
896450 | Bart Dhoedt
896450 | Marc De Leenheer
你能解釋'string_agg(name,'')'和'concat(name)'之間的區別嗎? – Patryk
@Patryk更新 –