Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

group concat - MySQL Group_Concat Repeating Values

I am working on an open source project called PHP-Bouncer, and I'm having issues with a MySQL Query I am writing for it. Basically we have three tables: BouncerRoles, PageInRole, and BouncerPageOverrides. BouncerRoles contains access levels, and the other two tables link back to BouncerRoles via Foreign Key and provide multiple entries of additional data. I have written the following query to attempt to pull all of the role data I need all at once:

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;

The Goal of this query is to provide the RoleID, RoleName, a pipe delimited list of provided pages, and a pipe delimited list of overrides (in the form of overriddenpage&overridingpage). Everything works except the last column of the query, which repeats the entries it finds over and over like this (output in CSV format):

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

Is there something I've done wrong in my query to cause this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are probably joining a table with two tables on 1..n relationships, producing duplicate results.

  • Use either GROUP_CONCAT( DISTINCT ...) or

  • Use two subqueries: in each one use GROUP_CONCAT() with group by on each of the 2 tables. Then join the two subqueries and the main table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...