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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:27:09+00:00 2026-06-16T04:27:09+00:00

I am implementing Add Friend in my web app, so users could add other

  • 0

I am implementing “Add Friend” in my web app, so users could add other users as fiends.

We have 2 tables: tbl_users and tbl_relations, tbl_users have an unique ID for the registered users and tbl_relations will store the users which are friends, for example some rows in tbl_relations are:

id     user_id_1     user_id_2
1      4             6
2      4             8
3      8             23
4      12            84
5      3             4
...

In the above results, the id is the unique id for the tbl_relations, user_id_1 is foreign key to tbl_users and user_id_2 is foreign key to tbl_users, now imagine we want to query and check if user with id “4” is friend with user with id “9” or not, here we need to send the query in 2 ways, I mean:

SELECT * FROM tbl_relations WHERE (user_id_1 = '4' AND user_id_2 = '9') OR (user_id_1 = '9' AND user_id_2 = '4')

The above query seems a little weird to me, there should be another way for implementing this I guess, maybe a different database structure?

Or another query, we want to get the mutual friends between users with id “4” and “8”, how I’m supposed to get the mutual friends in this scenario? is there any better database structure for this?

I would appreciate any kind of help.

  • 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-16T04:27:10+00:00Added an answer on June 16, 2026 at 4:27 am

    I would de-normalize the relation such that it’s symmetric. That is, if 1 and 2 are friends, i’d have two rows (1,2) and (2,1).

    The disadvantage is that it’s twice the size, and you have to do 2 writes when forming and breaking friendships. The advantage is all your read queries are simpler. This is probably a good trade-off because most of the time you are reading instead of writing.

    This has the added advantage that if you eventually outgrow one database and decide to do user-sharding, you don’t have to traverse every other db shard to find out who a person’s friends are.

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

Sidebar

Related Questions

Before implementing j_security_check using MySQL realm authentication in my web app. I had the
I'm implementing a RESTful web service in python and would like to add some
I have gotten round to implementing the ADD A,r set of opcodes on my
I am implementing facebook posting in my app. And add some code to post
I am implementing an application that allows users to add and share different documents.
I am implementing a service that lets users browse various objects, add new ones
I'm implementing jQuery Sparklines plugin for a web app I'm developing. I want to
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I am implementing a Twitter app, where the app needs to cache details of
I am implementing a shopping cart site. When the user add the item, it

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.