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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:41:31+00:00 2026-05-22T01:41:31+00:00

I have persons and a person can contact multiple other persons, so basically the

  • 0

I have persons and a person can contact multiple other persons, so basically the “default” tables would be:

persons (id)
contacts (person1_id, person2_id)

With this schema, I’d have to issue queries like

SELECT   * 
FROM     contacts c
WHERE    ( person1_id = *id of person1* AND person2_id = *id of person2* )
         OR
         ( person1_id = *id of person2* AND person2_id = *id of person1* )

to get the relation between two persons when I insert such a relation only once.

What is the common practice to deal with this situation?

  1. Insert data once and do such an OR query
  2. Insert the relation twice so that person1_id = id of person1 AND person2_id = id of person2 is enough
  3. An entirely different approach?

Assuming:

  • The m:n table actually contains additional data, so if I create a relation for both ways, I’d have to duplicate the data
  • This is a core part of the application and most non-trivial queries involve at least a sub query that determines whether or not such a relation exists
  • 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-22T01:41:32+00:00Added an answer on May 22, 2026 at 1:41 am

    If you write your insert logic such that person1_id < person2_id is true for all rows, then you can just write

    SELECT *
    FROM contacts c
    WHERE person1_id = min(*id_of_person_1*, *id_of_person_2*)
    AND person2_id = max(*id_of_person_1*, *id_of_person_2*)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Person class. A Person can have an associated control. Can I
I have a class Person which can have several Homes, each one with one
Situation: I have some persons with certain skills and they can/might belong to more
I have a very similar situation to the person who asked: Can I serve
I have some code like this: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Save([Bind(Prefix=)]Person person) { String s
I have a model called Kase each Case is assigned to a contact person
Who is/are the best contact points for Play Framework? Can we have email addresses?
http://img256.imageshack.us/img256/9417/snapshotapp.jpg http://img256.imageshack.us/img256/9417/snapshotapp.jpg Contact Title has a default value which is Sales Person. Anyhow, when
I have a combobox of Names. There can be person with similar name. Now
I have clients and contacts, and each client can have any number of contacts

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.