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 8730677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:04:25+00:00 2026-06-13T09:04:25+00:00

I have two tables: table_a: table_b +—-+——+ +—-+———+——+ | id | name | |

  • 0

I have two tables:

table_a:            table_b
+----+------+    +----+---------+------+
| id | name |    | id | relation| name | 
+----+------+    ++++++---------+------+
| 1  | bob  |    | 1  | friend  | chris|
| 2  | jim  |    | 1  | friend  | jon  |
| 3  | tom  |    | 1  | brother | matt |
+----+------+    | 2  | friend  | sam  |
                 | 2  | parent  | ron  |
                 +----+---------+------+

and I want to enter a query to output something like

+----+------+------------+---------+--------+
| id | name |friend      | brother | parent |
+----+------+------------+---------+--------+
| 1  | bob  | chris, john| matt    |        |
| 2  | jim  | sam        |         |  ron   |
+----+------+------------+---------+--------+

So the id is the comman variable between the two tables,
the relation variables have preset values (either friend, brother, parent and maybe a couple other types), and there can be multiple table_b.name per relation per id.

Is this too complicated a task to do?

  • 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-06-13T09:04:26+00:00Added an answer on June 13, 2026 at 9:04 am
    SELECT a.ID, a.name,
            GROUP_CONCAT(CASE WHEN relation = 'friend' THEN b.name ELSE NULL END) friend,
            GROUP_CONCAT(CASE WHEN relation = 'brother' THEN b.name ELSE NULL END) brother,
            GROUP_CONCAT(CASE WHEN relation = 'parent' THEN b.name ELSE NULL END) parent
    FROM  table_a a
            INNER JOIN table_b b
                ON a.id = b.id
    GROUP BY a.ID, a.name
    

    SQLFiddle Demo

    in future, if you have any other relation other than friend, brother, and parent and you don’t want to alter the query, you can use prepared statement

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'GROUP_CONCAT(CASE WHEN relation = ''',
          relation,
          ''' then b.name ELSE NULL end) AS ',
          relation
        )
      ) INTO @sql
    FROM table_b;
    
    SET @sql = CONCAT('SELECT a.ID, a.name, ', @sql, ' 
                       FROM  table_a a
                             INNER JOIN table_b b
                                 ON a.id = b.id
                       GROUP BY a.ID, a.name');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    SQLFiddle Demo

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

Sidebar

Related Questions

I have two tables: Table_A name1 create_date Table_B name2 keep_time name1 is something like
I have two similar tables table_a and table_b table_a is my current data and
I have two tables, table_a and table_b: table_a: city -------- tokoyo london table_b: zipcode
I have two tables TABLE_A and TABLE_B having the joined column as the employee
I have two tables: TABLE_A TABLE_B Fields: Trans Amend Trans Amend data: 100 0
say you have two tables table_a f1 <- PKEY f2 f3... table_b b1 <-
I have two tables Table_A and Table_B and i want to join those tables
I have two tables, for example: Table A Table B ======= ======= Name |
I have two tables: Table A: id name ------------ 1 Scott 2 Dan 3
I have two tables, like so: table a contains: id|name stock1|fullname stock2|fullname2 stock3|fullname3 table

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.