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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:48:12+00:00 2026-06-02T18:48:12+00:00

I have two tables: User and User2Friend . I need to get user’s friends.

  • 0

I have two tables: User and User2Friend. I need to get user’s friends. Query should handle two columns as FriendId: FriendId and UserId because we don’t know who added whom. In result both are friends.

The query:

SELECT DISTINCT
    `Id`
    , `UserName`
FROM `User`
WHERE `Id` IN
    (SELECT
        IF(`UserId` = 25, `FriendId`, `UserId`) -- This important part - friends are always mutual
    FROM `User2Friend`
    WHERE `UserId` = 25
    OR `FriendId` = 25)
LIMIT 10

The schema:

CREATE TABLE `User` (
  `Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `UserName` varchar(16) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `User2Friend` (
  `Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `UserId` int(10) unsigned NOT NULL,
  `FriendId` int(10) unsigned NOT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `UserIdFriendId` (`UserId`, `FriendId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

If user A added user B as friend, user B appears in user A friend list, after that user B must have user A in friend list too. I don’t really like subquery approach because I cannot use LIMIT in sub-select.

I’m expecting few millions of users, so performance is important.

Do you think it would be better to split this query to two separate ones?

Or maybe I need to have tricky JOIN?

  • 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-02T18:48:14+00:00Added an answer on June 2, 2026 at 6:48 pm

    Okay! I have split it to two queries…

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

Sidebar

Related Questions

I have two tables user (one) and transaction (many) and I need to get
I have two mysql tables: USER and ROSTER. Previously, I've been able to query
I have two separate tables both with user id columns uid . I want
I have two tables to track user activity: I need to write a SQL
Need help figuring this out. I have two tables user ---- name skills (comma
I have two tables, in which one user can have several contacts: User(UserId, UserName)
I have two tables: Personnel with ID,Name,... columns and User with PersonnelID (FK of
I have two tables like User and UserInfo UsersInfo UserId UserName Email Password LastLoginDate
I have two tables with these columns: Users: user, class, grade, location. Classes: class,
I have two tables: tblBadge ------- ID Name tblBadgeUsers ------- BadgeID UserID A user

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.