Bre*_*gan 10 mysql group-concat
我正在开发一个名为PHP-Bouncer的开源项目,我正在为我正在编写的MySQL Query遇到问题.基本上我们有三个表: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;
Run Code Online (Sandbox Code Playgroud)
此查询的目标是提供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
Run Code Online (Sandbox Code Playgroud)
在我的查询中有没有我做错了导致这个?
ype*_*eᵀᴹ 29
您可能正在加入一个在1..n关系上有两个表的表,从而产生重复的结果.
使用GROUP_CONCAT( DISTINCT ...)或
使用两个子查询:在每个子查询中,每个子GROUP_CONCAT()表使用group by.然后加入两个子查询和主表.
| 归档时间: |
|
| 查看次数: |
4444 次 |
| 最近记录: |