【问题标题】:MySQL Group_Concat Repeating ValuesMySQL Group_Concat 重复值
【发布时间】:2012-07-14 05:39:24
【问题描述】:

我正在开发一个名为 PHP-Bouncer 的开源项目,我正在为它编写的 MySQL 查询遇到问题。基本上我们有三个表:BouncerRoles、PageInRole 和 BouncerPageOverrides。 BouncerRoles 包含访问级别,另外两个表通过外键链接回 BouncerRoles 并提供多个附加数据条目。我编写了以下查询来尝试一次提取我需要的所有角色数据:

select BouncerRoles.RoleID, BouncerRoles.RoleName, 
GROUP_CONCAT(PageInRole.PageName separator '|') as ProvidedPages, 
GROUP_CONCAT(CONCAT(BouncerPageOverrides.OverriddenPage,'&',BouncerPageOverrides.OverridingPage) separator '|') as OverriddenPages 
from BouncerRoles join PageInRole on BouncerRoles.RoleID = PageInRole.RoleID 
join BouncerPageOverrides on BouncerRoles.RoleID = BouncerPageOverrides.RoleID
group by BouncerRoles.RoleID;

此查询的目标是提供 RoleID、RoleName、管道分隔的已提供页面列表和管道分隔的覆盖列表(以覆盖页面和覆盖页面的形式)。一切正常,除了查询的最后一列,它会像这样一遍又一遍地重复找到的条目(以 CSV 格式输出):

RoleID,RoleName,ProvidedPages,OverriddenPages
2,Exchange,exchange-how.php|exchange-support.php|exchange.php|premium-promo.php|exchange-resorts.php|premiumplus-promo.php|exchange-deposit.php|exchange-requestdestination.php,whyexchange.php&exhange.php|whyexchange.php&exhange.php|whyexchange.php&exhange.php|whyexchange.php&exhange.php|whyexchange.php&exhange.php|whyexchange.php&exhange.php|whyexchange.php&exhange.php|whyexchange.php&exhange.php
3,Premium,premiumplus-promo.php|premium-cruises.php|premium-resorts.php|premium-condohome.php|premium-hotelaircar.php|premium.php|premium-restaurants.php|premium-overview.php,premium-promo.php&premium.php|premium-promo.php&premium.php|premium-promo.php&premium.php|premium-promo.php&premium.php|premium-promo.php&premium.php|premium-promo.php&premium.php|premium-promo.php&premium.php|premium-promo.php&premium.php
4,"Premium Plus",premiumplus-exclusiveescapes.php|premiumplus.php|premiumplus-overview.php|premiumplus-concierge.php|premiumplus-airportlounge.php,premiumplus-promo.php&premiumplus.php|premiumplus-promo.php&premiumplus.php|premiumplus-promo.php&premiumplus.php|premiumplus-promo.php&premiumplus.php|premiumplus-promo.php&premiumplus.php

是我在查询中做错了什么导致了这种情况吗?

【问题讨论】:

  • 请注意GROUP_CONCAT 如果你有一个很大的结果可能会让人头疼 -> 它只会返回一个有限大小的结果(我认为是 1024 字节,但我不确定),所以如果你的结果集更大,它会被切断。
  • 您的GROUP_CONCAT 中是否缺少DISTINCT?您的联接似乎返回了多行。
  • @nanne:该限制 (1024) 取决于设置。可以加长。
  • 没错,但你仍然需要知道它是否存在:)。另外,但这只是我的看法,要求您的用户更改他们的设置可能会很棘手......
  • 我之前没有收到任何答复:( dba.stackexchange.com/questions/40266/…

标签: mysql group-concat


【解决方案1】:

您可能在 1..n 关系上加入一个包含两个表的表,从而产生重复的结果。

  • 使用GROUP_CONCAT( DISTINCT ...) 或

  • 使用两个子查询:在每个子查询中使用 GROUP_CONCAT() 并在 2 个表中的每一个上使用 group by。然后加入两个子查询和主表。

【讨论】:

  • 将 Distinct 添加到第二个 group_concat 成功了...谢谢!
  • 没有其他选择吗?与大型数据集(在我的情况下)一样,子查询非常慢,但我不能使用 distinct,因为我需要返回每个值,即使它是相同的..
  • @ThomasClowes 你可以发布一个问题。不要忘记包含实际查询、表的定义(包括索引)和执行计划。您也可以在http://dba.stackexchange.com/ 上发布问题,这样可能会得到更好的关注。
  • 我以前做过,但没人能帮忙:(dba.stackexchange.com/questions/40266/…
  • @ThomasClowes 我在那里没有看到查询。很难理解你到底想要什么。 ACN 你加了吗?
猜你喜欢
  • 2011-06-01
  • 2015-07-14
  • 1970-01-01
  • 2017-08-25
  • 2013-08-03
  • 2016-05-26
  • 2016-12-21
  • 2013-11-21
  • 2012-09-24
相关资源
最近更新 更多