我有一张表格,显示一个人对学校的偏好,比如
Person(name, preference_id)
/
School(school_id, name)
每个人都可以对多所学校感兴趣(由preference_id显示),如下所示:
Person(John, 132)
Person(John, 133)
School(132, UVA)
School(133, UMBC)
^John would like to go to either UVA or UMBC.
我想写一个查询,显示每个人的所有school.name s。
select person.name, school.name
from Person, School
where person.preference_id = school.school_id
;
但我上面所说的将以1人1的学校名称比例返回。无论如何,我可以修改这个,所以它是1个人 - 多学校名?感谢
答案 0 :(得分:1)
SELECT p.name,
LISTAGG( s.name, ',' ) WITHIN GROUP ( ORDER BY s.name ) AS preferences
FROM Person p
INNER JOIN School s
ON ( p.preference_id = s.school_id )
GROUP BY p.name