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

  • SEARCH
  • Home
  • 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 695431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:57:31+00:00 2026-05-14T02:57:31+00:00

I want to write a mysql statement which will return a list of results

  • 0

I want to write a mysql statement which will return a list of results from one table along with a comma separated list of field from another table. I think an example might better explain it

Table 1
========================

id First_Name Surname
----------------------
1  Joe       Bloggs
2  Mike      Smith
3  Jane      Doe

Table 2
========================

id Person_Id Job_id
---------------------
1  1         1
2  1         2
3  2         2
4  3         3
5  3         4

I want to return a list of people with a comma separated list of job_ids. So my result set would be

id First_Name Surname job_id
------------------------------
1  Joe       Bloggs   1,2
2  Mike      Smith    2
3  Jane      Doe      3,4

I guess the sql would be something like

select id, First_Name, Surname, (SELECT job_id FROM Table 2) as job_id from Table 1

but obviously this does not work so need to change the ‘(SELECT job_id FROM Table 2) as job_id’ part.

Hope this makes sense

Thanks
John

  • 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-14T02:57:32+00:00Added an answer on May 14, 2026 at 2:57 am

    You may want to use the GROUP_CONCAT() function, as follows:

    SELECT    t1.id, 
              t1.first_name, 
              t1.last_name,
              GROUP_CONCAT(DISTINCT job_id ORDER BY job_id SEPARATOR ',') job_id
    FROM      Table1 t1
    JOIN      Table2 t2 ON (t2.Person_id = t1.id)
    GROUP BY  t1.id;
    

    Let’s test it with your example data:

    CREATE TABLE Table1 (
        id int AUTO_INCREMENT PRIMARY KEY, 
        first_name varchar(50), 
        last_name varchar(50));
    
    CREATE TABLE Table2 (
        id int AUTO_INCREMENT PRIMARY KEY, 
        person_id int,
        job_id int);
    
    INSERT INTO Table1 VALUES (NULL, 'Joe', 'Bloggs');
    INSERT INTO Table1 VALUES (NULL, 'Mike', 'Smith');
    INSERT INTO Table1 VALUES (NULL, 'Jane', 'Doe');
    
    INSERT INTO Table2 VALUES (NULL, 1, 1);
    INSERT INTO Table2 VALUES (NULL, 1, 2);
    INSERT INTO Table2 VALUES (NULL, 2, 2);
    INSERT INTO Table2 VALUES (NULL, 3, 3);
    INSERT INTO Table2 VALUES (NULL, 3, 4);
    

    Result of the query:

    +----+------------+-----------+--------+
    | id | first_name | last_name | job_id |
    +----+------------+-----------+--------+
    |  1 | Joe        | Bloggs    | 1,2    | 
    |  2 | Mike       | Smith     | 2      | 
    |  3 | Jane       | Doe       | 3,4    | 
    +----+------------+-----------+--------+
    

    Note that by default, the result of GROUP_CONCAT() is truncated to the maximum length of 1024. However this can be set to a much larger value. Use the SET command if you require to modify it, as follows:

    SET GLOBAL group_concat_max_len = 2048;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a Mysql statement that selects all from a table(posting) where
I have a table with a CLOB field (MySQL MediumText). I want to return
I want write this sql select statment in mysql v 3.23 select * from
I want to write mysql when statement and i can't do it. explanation =>
In MySQL table, I have a field called Tag, which may include multiple comma-delimited
I want to write some JavaScript that will change the onmousedown of a div
This one really has me stumped. I have a documents table which hold info
I'm currently trying to write a simple ORM with PHP and mysql. I want
I want to write a SQL statement something like insert into mytable column1=value1 and
I want to write a MySql query whereby I want to use combination of

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.