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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:45:17+00:00 2026-06-16T23:45:17+00:00

This may have been asked before and I just do not know the way

  • 0

This may have been asked before and I just do not know the way to search for it. I think this should be basic but I can’t figure it out.

I have a friends table that is formatted as below:

id   toid  fromid
1     45     50
2     50     45
3     46     45
4     45     46

5     40     45

The member logged in is $member_number…which in this case is “45”.

The "toid" and "fromid" fields correspond to member IDs. If the member “45” is logged in, how would I write a SELECT query to get all the members that they are friends with?

In order for them to be friends there has to be reciprocating friend rows. For example, in the above, members 45 and 50 are friends because they friend records going to and from (in rows 1 and 2). The same can be said of 45 and 46 as shown in rows 3 and 4. But, 40 and 45 are not friends because there are not reciprocating rows.

So the result of the Select statement should be… 50, 46.

  • 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-16T23:45:18+00:00Added an answer on June 16, 2026 at 11:45 pm

    You could use a union of two selects, like this

    select fromid from <table> where toid = $member_number
    union
    select toid from <table> where fromid = $member_number
    

    union has the extra benefit of filtering out duplicates so in your example 50 won’t show up twice.

    Make sure both toid and fromid are indexed (individually or as the left most part of another index) and this query will go lightning fast.

    Also, there is no need for the id column (in my opinion), you put a primary key on toid and fromid and that is enough to identify any row in this table.

    An example can be seen at SQLFiddle

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

Sidebar

Related Questions

First, this question may have been asked before, but I'm not sure what phrase
this may have been asked before, but I could not find it. Suppose I
This question may have been asked before, but it's not in the same manner.
This may have been asked before, but it's really hard to search for terms
This may have been asked before, but I could not find the answer. I
This question may have been asked before, but I had trouble finding an answer,
This has / may have been asked before, but, as far as I remember,
This question may have been asked before, but I'm starting to get into game
this question may have been asked many times, but could not find any suitable
I'm aware that this question may have been asked before, but I still haven't

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.