Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1005571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:19:08+00:00 2026-05-16T08:19:08+00:00

Lets say I have 2 tables: 1 with users and another one which keeps

  • 0

Lets say I have 2 tables:

1 with users
and another one which keeps record of which users used what codes.

Users
-----
Id, Name
1, 'John'
2, 'Doe'

Codes
------
Id, UserId, code
1, 1, 145
2, 1, 187
3, 2, 251

Now I want to pull a query that results he following

Name, UsedCodes
'John', '145,187'
'Doe', '251'

How can this be done with a query or stored procedure?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:19:08+00:00Added an answer on May 16, 2026 at 8:19 am

    Since you haven’t specified the DB I’m giving you two options:

    MySql

    With MySql you should simply use GROUP_CONCAT() aggregate function.

    Microsoft SQL Server 2005+

    Obviously the fastest way (no cursors, no coalesce…) of getting the same result on MS DB is by using FOR XML PATH('') that simply omits XML elements.

    SELECT
        u.Name,
        c1.UserId,
        (
            SELECT c2.Code + ','
            FROM Codes c2
            WHERE c2.UserId = c1.UserId
            ORDER BY c2.code
            FOR XML PATH('')
        ) as Codes
    FROM Codes c1
        JOIN Users u
        ON (u.Id = c1.UserId)
    GROUP BY c1.UserId, u.Name
    

    Other alternatives

    Read this article, that explains all the possible ways of achieving this goal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have two tables - child and parent with many-to-one relation. What
Let's say you have two tables, Users and UserRoles. Here's how the two tables
Let's say I have four tables: PAGE , USER , TAG , and PAGE-TAG
Lets say I have a table in a sql server 2000 database called TransactionType:
Let's say I have two tables: Report Comment And assuming I have a database
Let's say I have two tables, news and comments. news ( id, subject, body,
Scenario: Let's say I have two tables, TableA and TableB. TableB's primary key is
Let's say I have two existing tables, dogs and cats: dog_name | owner ---------+------
Let's say I have a table like this: name | score_a | score_b -----+---------+--------
Let's say I have a table that represents a super class, students . And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.