gpt4 book ai didi

mysql - 带有 GROUP_CONCAT 的 INNER JOIN 返回重复值

转载 作者:行者123 更新时间:2023-11-29 06:36:42 25 4
gpt4 key购买 nike

如果我尝试使用以下查询来查询数据库,我会得到预期的结果:

SELECT anime.anime, GROUP_CONCAT(themes.theme) AS themes
FROM anime
INNER JOIN anime_themes ON anime.id = anime_themes.anime_id
INNER JOIN themes ON themes.id = anime_themes.theme_id
GROUP BY anime.anime
ORDER BY anime.id;

示例输出:“失忆症、 body 转换、自然灾害、悲剧”

但是,当我尝试使用以下查询一次查询更多数据时,我得到了重复数据:

SELECT anime.*, directors.director, studios.studio, 
GROUP_CONCAT(genres.genre) AS genres, GROUP_CONCAT(themes.theme) AS themes
FROM anime
INNER JOIN anime_directors ON anime.id = anime_directors.anime_id
INNER JOIN directors ON directors.id = anime_directors.director_id
INNER JOIN anime_studios ON anime.id = anime_studios.anime_id
INNER JOIN studios ON studios.id = anime_studios.studio_id
INNER JOIN anime_genres ON anime.id = anime_genres.anime_id
INNER JOIN genres ON genres.id = anime_genres.genre_id
INNER JOIN anime_themes ON anime.id = anime_themes.anime_id
INNER JOIN themes ON themes.id = anime_themes.theme_id
GROUP BY anime.anime
ORDER BY anime.id;

示例输出: body 开关,自然灾害,悲剧,失忆症, body 开关,自然灾害,自然灾害,悲剧,失忆症,失忆症, body 开关,自然灾害,悲剧,悲剧,失忆症, body 开关“

数据似乎甚至没有以某种连贯的顺序与第二个查询重复。为什么两个查询之间的行为不同,我该如何解决这个问题?谢谢。

编辑:只有“主题”和“流派”有重复值,而“导演”和“工作室”则没有,所以我认为它与 GROUP_CONCAT 函数有关。

最佳答案

只需添加 DISTINCT。

示例:

    GROUP_CONCAT(DISTINCT genres.genre) AS genres, GROUP_CONCAT(DISTINCT 
themes.theme) AS themes

关于mysql - 带有 GROUP_CONCAT 的 INNER JOIN 返回重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53530343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com