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
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 -
SELECT users.user_id, firstname, GROUP_CONCAT( groups.group_name )
-
FROM users
-
JOIN (
-
user_groups, groups
-
) ON ( users.user_id = user_groups.user_id
-
AND user_groups.group_id = groups.group_id )
-
GROUP BY users.user_id, firstname
-
LIMIT 0 , 30
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)
|
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
|
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
|
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"
|
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
,
| |
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
|
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, "", ""
}
|
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
|
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...
|
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
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |