473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MySQL - need to concatenate results of subquery in a select statement

1
I have three tables
Table1: Users
Columns: User_ID (int), FirstName, LastName....
Values:
1 Jane Doe
2 John Doe
3 Mike Smith

Table2: User_Groups
Columns: User_ID (int), Group_ID(int)
Values:
1 2
1 3
2 1
2 3
3 1

Table3: Groups
Columns: Group_ID (int), GroupName(varchar)
Values:
1 Admin
2 Power User
3 Developer

I would like to create a query that can return the results in the following way:
**RESULT
UserID GroupNames
1 Power User, Developer
2 Admin, Developer
3 Admin

In SQL Server - I was able to achieve it using something like this:
SELECT User_ID,
SUBSTRING(
replace(
replace(
(SELECT Groups.GroupName
FROM User_Groups, Groups
where groups.Group_ID =
User_Groups.Group_ID AND
User_Groups.User_ID =Users.User_ID
FOR XML PATH('') )
,'<GROUPNAME>',', ')
,'</GROUPNAME>',''),3,2000) as UserGroups
FROM User_Groups LEFT JOIN Groups ON
User_Groups.Group_ID=Groups.Group_ID
ORDER BY User_ID ASC

I wanted to do get a similar final result in MySQL (tried GROUP_CONCAT etc) but unsuccessful.. how can I get similar **RESULT in MySQL. Please note the tables exist already and I cant change them.
Any help will be greatly appreciated
Oct 8 '10 #1
1 6546
JKing
1,206 Expert 1GB
Hi, GROUP_CONCAT is the way to go. It is important to remember to use the GROUP BY clause when you use an aggregate function such as GROUP_CONCAT

Expand|Select|Wrap|Line Numbers
  1. SELECT users.user_id, firstname, GROUP_CONCAT( groups.group_name )
  2. FROM users
  3. JOIN (
  4. user_groups, groups
  5. ) ON ( users.user_id = user_groups.user_id
  6. AND user_groups.group_id = groups.group_id )
  7. GROUP BY users.user_id, firstname
  8. LIMIT 0 , 30
  9.  
Oct 13 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Puffer Fish via SQLMonster.com | last post by:
Hi, I want to create a select statement showing --> John -- happy -- Alice -- Mad -- Sunday John -- Mad -- Alice -- happy -- Monday Here is my table structure: main (table)
3
by: elubin_nospam | last post by:
Given these two tables in DB/2: Date Activity ------------------- -------- 2005-01-02 00:00:00 1 2005-01-05 00:00:00 2 2005-01-05 00:00:01 1 2005-01-07 00:00:00 2...
3
by: jodyblau | last post by:
I'm not certain if it is possible to do this, and if it is I don't know what the syntax would look like. I am currently using a statement like so: Set rs1 = CurrentDb.OpenRecordset("Select *...
1
by: luvic.vangool | last post by:
Hi, I'm fairly new to MySql and come from a MS SQL background. I have an application that I am converting from MS SQL to MySql. I need to return a value from MySql using a parameter. In MS...
2
by: rhaazy | last post by:
using mssql 2000... Table Def... CREATE TABLE . ( IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL , (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
0
by: crljenica | last post by:
I need to turn this select statement into a delete statemen. In other words, I want the results of the select statement deleted from the tables. I have never used a delete statement with a minus...
1
by: anthonyjm | last post by:
I need to run a report for each record returned from a SQL Select statement. The report changes for each record, so they need to be run separately. So something like this (I have no idea what the...
21
by: bruno_guedesav | last post by:
I've made a function to fetch all results as an array of result- arrays. Getting the result arrays is easy, via mysql_fetch_array, and function itself is quite simple, as follows: function...
2
by: Path9898 | last post by:
My application runs a series of webpages (like a slideshow) using data out of a mySQL DB. The first page that loads runs a select statement and writes the results to a cookie. The cookie then...
0
by: DJN001 | last post by:
I need to perform a select statement that compares a literal to a timestamp.. like select * from table1 where timestampfld > 20100330
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.