我试图在一个字符串中加入两个多对多关联。在此示例中,每个团队都有未确定数量的颜色和未确定数量的获奖者。
这是架构:
这是我正在使用的查询:
SELECT
teams.name AS name,
GROUP_CONCAT(colours.name) AS colours,
GROUP_CONCAT(awards.name) AS awards
FROM
teams
-- join colours
INNER JOIN teams_to_colours
ON teams.id = teams_to_colours.team_id
INNER JOIN colours
ON teams_to_colours.colour_id = colours.id
-- join awards
INNER JOIN teams_to_awards
ON teams.id = teams_to_awards.team_id
INNER JOIN awards
ON teams_to_awards.award_id = awards.id
WHERE
teams.name="A-Team"
GROUP BY
teams.id
问题是颜色和奖项会重复。假设 A-Team 有红色和蓝色作为颜色,作为奖项 TrollAward 和 DarwinAward ......我从 SQL 得到的结果如下所示:
name: "A-Team"
colours: "red,blue,red,blue"
awards: "TrollAward,DarwinAward,TrollAward,DarwinAward"
我试图只加入一个多对多,并且工作得很好,所以我想我正在监督多个加入的东西......