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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:53:07+00:00 2026-05-26T03:53:07+00:00

I have a question about MySQL. I have this table structure: ID Name ——

  • 0

I have a question about MySQL. I have this table structure:

ID      Name
------  ------------
1       Peter
2       John
3       Carl
4       William

I need to link the record.
I.e.
– Peter is related to Carl and William
– Carl is related to John

Should I make a new table like this:

ID1     ID2
------  ------------
1       3
1       4
3       2

Or should I extend the first table like this:

ID      Name          Links
------  ------------  ----------
1       Peter         3,4
2       John
3       Carl          2
4       William

In both cases, how do I make a query that returns:

Name          LinkedName
------------  --------------
Peter         Carl
Peter         William

I have considered to use JOIN, UNION and sub-queries, but I can not really get it to work.
I really hope that somebody can help me here.

Thanks.

  • 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-26T03:53:07+00:00Added an answer on May 26, 2026 at 3:53 am

    As it is an n:m relationship (“Peter is related to Carl and William”), your first idea is the right one: an additional table with two IDs. You could add further information about the relationship there. Also add foreign keys and a primary key to prevent duplicate entries.

    CREATE TABLE rel (
        from_id integer references person(id),
        to_id integer references person(id),
        primary key(from_id, to_id)
    );
    

    Query like this:

    SELECT p1.name, p2.name AS linked_name
      FROM person p1
      JOIN rel r ON (r.from_id = p1.id)
      JOIN person p2 ON (r.to_id = p2.id)
     WHERE p1.name = 'Peter';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you have any experience about this question? I have currently 1900 MySQL databases
I would like to ask something about query using mysql I have this table
I have a question about MySql. I have a table with 7.479.194 records. Some
I have a question about tables in MySQL. I'm currently making a website where
I have a question about locking. This doesn't have to be only about record
I have a question about converting charset from inside mysql query. I have a
i have a question about basic mysql database optimisation. I have 3 tables, Articles,
I have a question about how to structure a DB. I have a reddit'esque
Please note that this question is not about the mysql query per se, but
Please note that this question is not about the mysql query per se, but

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.