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

The Archive Base Latest Questions

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

I have a table in my database used for users to follow eachother. The

  • 0

I have a table in my database used for users to follow eachother.

The table goes as follows:

UniqueID
FollowerID
FollowedUserID

FollowID is the UserID of the person who is following someone else.

FollowedUserID is the UserID of the person who is being followed by the FollowID User.

I’d like to retrieve a list of connections from this table based off a single user. My query should return all the other UserIDs that the current user is either following or being followed by with no overlaps.

So say we have a few entries here:

FollowerID   FollowedUserID
    1              2
    1              3
    4              1
    2              1

This would show that the User 1 is following users 2 and 3, and thus users 2 and 3 are being followed by user 1. On the other side of the spectrum it shows that User 1 is followed by users 2 and 3.

What I’d like to do is find out the connections that user 1 has, so the query should return users: 2, 3, and 4 (user 1 is following users 2 and 3 and is being followed by user 4)

How can I achieve this from a single query?

A connection is considered either being followed or following someone else, so it is possible that some duplicate results can occur (if both users are following eachother). I’d like to be able to group these results so that the result is distinct.

I’ve tried a UNION query similar to:

SELECT FollowerID, FollowedUserID From Follows WHERE FollowerID = 1
UNION
SELECT FollowerID, FollowedUserID FROM Follows WHERE FollowedUserID = 1

Theoretically I’d like to Group FollowerID and FollowedUserID together to keep them distinct.

I’m not interested in retrieving a non-distinct result and then creating a dataset of unique results php side — the query should return distinct values only.

  • 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:45:05+00:00Added an answer on May 22, 2026 at 1:45 am

    You were close, but you have to reverse the order of the columns in one of the branches of the UNION:

    SELECT FollowerID AS reference_user, FollowedUserID AS connection
      FROM Follows
     WHERE FollowerID = 1
    UNION
    SELECT FollowedUserID AS reference_user, FollowerID AS connection
      FROM Follows
     WHERE FollowedUserID = 1
    GROUP BY reference_user;
    

    Note that if you drop the WHERE clauses, you get all connections for everyone.

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

Sidebar

Related Questions

I have a table in a database which is used for storing application configuration
i have database table like this +-------+--------------+----------+ | id | ip | date |
i have a database table of website links. i have another table of tags,
I have a table in database that is having some fields one of which
that my problem: I have database table like that: id (AI) market_id 1 6
I have one database table which contains 8 columns. One of the columns is
I have a database table using an enum. This is already working with hibernate
I have a database table where the user marks files to be downloaded. Subsequently,
I have a database table with a field that I need to read from
I have a database table of zipcodes with their Lat/Longs. I'm trying to find

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.