这个问题是我之前的问题的延续,可以在这里找到。
这个SQLFiddle正在使用我在下面描述的数据库结构和查询。
数据库如下所示:
CREATE TABLE artistnames (
artistname_id SERIAL PRIMARY KEY,
artistname TEXT UNIQUE NOT NULL
);
CREATE TABLE artistalias (
artistalias_id SERIAL PRIMARY KEY,
artistname_id SERIAL REFERENCES artistnames (artistname_id),
artistalias TEXT UNIQUE NOT NULL
);
CREATE TABLE songs (
song_id SERIAL PRIMARY KEY,
song TEXT NOT NULL,
artistalias_id SERIAL REFERENCES artistalias (artistalias_id)
);
- 一位艺术家(表艺术家名称)可以有零个、一个或多个别名
- 一个别名(表 Artistalias)只属于一位艺术家
- 一首歌(桌歌)有一个或多个艺术家(并且指的是他用来表演这首歌的艺术家的别名)
示例:艺术家Francis Veigar还使用别名Francis Fat和Francis Fighter。一首歌曲Song 1以艺人名Francis Veigar发行,另一首歌曲Song 2他使用化名Francis Fat和第三首歌曲Song 3他使用化名Francis Fighter与另一位艺人Peeka Boo一起演唱。
使用查询
SELECT
string_agg(distinct(artistname), ' & ') AS artist_primary_name,
string_agg(distinct(a1.artistalias), ' & ') AS performed_song_with_alias,
string_agg(a2.artistalias, ' & ') AS other_pseudonymes,
song
FROM
artistalias a1
left JOIN artistalias a2 ON a2.artistname_id = a1.artistname_id
left JOIN songs s ON s.artistalias_id = a1.artistalias_id
left JOIN artistnames ON artistnames.artistname_id = a1.artistname_id
GROUP BY song;
显示other_pseudonymes
列
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| artist_primary_name | performed_song_with_alias | other_pseudonymes | song |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| Francis Veigar | Francis Veigar | Francis Veigar & Francis Fat & Francis Fighter | Song 1 |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| Francis Veigar | Francis Fat | Francis Veigar & Francis Fat & Francis Fighter | Song 2 |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| Francis Veigar & Peeka Boo | Francis Fighter & Peeka Boo | Francis Veigar & Francis Fat & Francis Fighter & Peeka Boo & Peeka | Song 3 |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| Peeka Boo | Peeka | Peeka Boo & Peeka | Song 4 |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
我希望它看起来像
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| artist_primary_name | performed_song_with_alias | other_pseudonymes | song |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| Francis Veigar | Francis Veigar | Francis Veigar & Francis Fat & Francis Fighter | Song 1 |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| Francis Veigar | Francis Fat | Francis Veigar & Francis Fat & Francis Fighter | Song 2 |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| Francis Veigar & Peeka Boo | Francis Fighter & Peeka Boo | Francis Veigar & Francis Fat & Francis Fighter / Peeka Boo & Peeka | Song 3 |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
| Peeka Boo | Peeka | Peeka Boo | Song 4 |
+----------------------------+-----------------------------+--------------------------------------------------------------------+--------+
用“/”分隔两位不同艺术家使用的假名/别名。在查询中必须改变什么来实现这一点?