Categories
Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Web Hosting

<< May, 2013 >>
SMTWTFS
1234
567891011
12131415161718
19202122232425
262728293031
Search Blog

Recent Comments
Re: Disappearing IE Popup Window During Save/Open Dialog (by LZ at 4/20 7:58 AM)
Re: Create Dynamic WHERE Clauses in PHP (by pooja at 3/20 7:29 AM)
Re: Just What IS a 'Service Layer', Anyway? (by EugenK at 3/07 7:56 PM)
Re: Using Google as your CF Mail Server (by 5starwebteam.com at 2/25 1:27 AM)
Re: Why Provide for Service layer objects in CFWheels? (by Steven Benjamin at 1/25 11:43 AM)
Re: What is an 'Advanced' Coldfusion Developer? (by ColdFusion Developer at 12/24 5:14 AM)
Re: Equivalent of SQL "TOP X" in Oracle (by Ashenafi Desalegn at 12/06 5:29 AM)
Re: PHP Export to Excel Snippet (by serene at 12/05 1:44 AM)
Re: Just What Is 'Application Logic', Anyway? (by Arif at 11/13 8:06 AM)
Re: Hosts File Changes Not Acknowledged on Vista 64 (by Aaron at 10/22 2:31 PM)
Re: PHP Export to Excel Snippet (by Jafar Shah at 10/10 4:28 AM)
Re: Viewing Option Text (in IE7) that's Wider than the Select List (by Chenelle S at 10/04 12:53 PM)
Re: PHP Export to Excel Snippet (by Kilo at 9/26 5:20 PM)
Re: Porting Coldfusion Code to Mura (by tariq at 9/03 9:51 AM)
Re: Just What IS a 'Service Layer', Anyway? (by James at 8/27 4:06 PM)
Re: Calculating Business Hours (by helen at 8/14 2:54 AM)
Re: What IS 'Business Logic', Anyway? (by dougboude at 8/06 11:30 AM)
Re: What IS 'Business Logic', Anyway? (by Adrianne at 8/06 10:29 AM)
Re: Family Law: The Weapon of Choice for Woman Scorned (by dougboude at 8/04 4:39 PM)
Re: Family Law: The Weapon of Choice for Woman Scorned (by Lola LB at 8/04 7:43 AM)
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

Powered by
BlogCFM v1.11

21 December 2009
A SWEET Little MySQL Function: Group_Concat

The Scenario

You're writing code to perform authentication for a web-based app. Your users live in one table (Site_User), your list of site permissions live in another table (systemRoles), and the two are related via a junction table (jctUserRole) that manages the many to many relationship there. Your backend database: MySQL version 5 or better.

So, in an ideal world, you will be able to write a single query that will both authenticate the user AND retrieve a comma delimited list of their roles contained in a single field; if their credentials are good, your result will only have a single record in it. Typically, however, because of the way many to many relationships are normalized, the "one query" you want will be returning multiple rows...one row for each permission the authenticating user is related to. For example, here is the typical way one would write the authenticating query:


  SELECT u.id AS userid, u.firstname, u.lastname, r.rolename
  FROM Site_User u
  INNER JOIN jctUserRole j ON j.userID = u.id
  INNER JOIN systemroles r ON r.id = j.roleID
  WHERE u.username =  'username'
  AND u.password =  'password'

 

This produces a result that looks like this:

standard mysql query for authentication

We would then be required to write additional code in order to more properly "package" up this user's list of permissions for use within the system.

But enter the wonderful world of MySQL 5, and the 'group_concat' function. Group_concat allows you to concatenate values into a list based on a specified grouping, and will produce a list of unique values delimited by whatever character you choose (but defaults to a comma). So transforming the above query just a little bit, we get:

 

  SELECT u.id AS userid, u.firstname, u.lastname, GROUP_CONCAT( r.rolename ) AS roles
  FROM Site_User u
  INNER JOIN jctUserRole j ON j.userID = u.id
  INNER JOIN systemroles r ON r.id = j.roleID
  WHERE r.username =  'username'
  AND r.password =  'password'
  GROUP BY u.id, u.firstname, u.lastname

 

And a result set that now looks like this:

mysql authentication query using group_concat

Man, SO much cleaner and nicer, eh? Now you can simply treat the "roles" value as a list...explode it into an array if you want, or leverage ColdFusion's myriad of List functions to interact with it.

Since discovering group_concat, I've found several other uses for it as well, particularly when producing reports for my internal clients...it saves me a lot of iterating over result sets in order to produce value summaries.

You can read the documentation for this function here




Posted by dougboude at 11:29 AM | PRINT THIS POST! |Link | 9 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: A SWEET Little MySQL Function: Group_Concat
I *love* group concat! Use it when querying my DB for indexing - it's a nice easy way to get items in one column.
Posted by todd sharp on December 21, 2009 at 12:11 PM

Re: A SWEET Little MySQL Function: Group_Concat
Yeah, that is a nice little MySQL function. I've wished I had it many times in MS SQL Server, but you have to compile your own C# code into a dll and register it as an assembly... kind of a pain if you using Microsoft. If I'm looking for a comma-delimited list for output in ColdFusion I've been known to take advantage of the fact that a query column can be treated as an array and shoved into arrayToList().
Posted by Brad Wood on December 22, 2009 at 1:23 AM

Re: A SWEET Little MySQL Function: Group_Concat
You could also fake it with FOR XML PATH. But it is still not as clean and neat as MySQL's group_concat function.
Posted by Leigh on December 22, 2009 at 5:50 AM

Re: A SWEET Little MySQL Function: Group_Concat
I found a similar set of 3rd party queries/procedures that does the same thing in Oracle. Unfortunately I had to create a variable and the queries in each database environment for it to work at the time. I've heard MS SQL has a built in function to do it. Either way, it is a nice feature to have at times and it surprises me it isn't standard in most databases.
Posted by Ryan Hartwich on December 23, 2009 at 2:26 PM

Re: A SWEET Little MySQL Function: Group_Concat
Fantastic function, wish I'd known about that years ago. Be aware though that by default the grouped list will be limited to 1024 characters. To allow longer values, you need to change or override the group_concat_max_len system variable.
Posted by Julian Halliwell on December 31, 2009 at 8:54 AM

Re: A SWEET Little MySQL Function: Group_Concat
Thanks For this Post its Very usefully for me in my current project and Its Help Lot to avoid lot of php coding.
Posted by Abin on June 11, 2010 at 2:37 AM

Re: A SWEET Little MySQL Function: Group_Concat
Thanks for sparing time
Posted by Dushyant on August 5, 2011 at 7:42 AM

Re: A SWEET Little MySQL Function: Group_Concat
Very informative blog! It is easy to see that you are impassioned about your writing. I wish I had got your ability to write. I look forward to more updates and will be returning.Thanks for sharing.
Posted by saraceejay on August 14, 2011 at 6:04 AM

Re: A SWEET Little MySQL Function: Group_Concat
how will work select for update in mysql
Posted by vinodkumar on May 31, 2012 at 4:53 AM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

Zero plus Fifteen equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!