473,657 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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(varch ar)
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.GroupNam e
FROM User_Groups, Groups
where groups.Group_ID =
User_Groups.Gro up_ID AND
User_Groups.Use r_ID =Users.User_ID
FOR XML PATH('') )
,'<GROUPNAME>', ', ')
,'</GROUPNAME>','') ,3,2000) as UserGroups
FROM User_Groups LEFT JOIN Groups ON
User_Groups.Gro up_ID=Groups.Gr oup_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 6676
JKing
1,206 Recognized Expert Top Contributor
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
1226
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
1455
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 2005-01-17 00:00:00 2 2005-02-02 00:00:00 1 2005-02-03 00:00:00 1
3
2225
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 * from tblJudges WHERE Name Like ""*" & myTerm & "*""", dbOpenDynaset) What I would like to do is build this select statement such that it
1
1488
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 SQL my select statement is: "select @id = id from database"
2
1184
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
1874
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 command. Can anyone help. SELECT item, ispecyr from itemlist where ispecyr = 79 minus select b.piitem, a.cpspecyr from contproj a, projitem b where a.pcn = b.pcn and cpspecyr = 79
1
5032
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 syntax would be): strSelect = "SELECT field1, field2, field3 FROM table1 WHERE field4 = '1';" results = DoCmd.RunSQL strInsert foreach results AS result { DoCmd.OpenReport "LibraryCard", acNormal, "", "" }
21
3696
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 db_query($db, $query) { $result = mysql_query($query, $db); $res_array = array(); if ($result) //it is a search
2
1675
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 provides the answers that the following pages need. Currently the select provides 26 values that goes into the cookie. This number is set to grow by 30%. Through out the slideshow there are about 4 additional smaller selects that provide answers for...
0
1352
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
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.