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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:33:14+00:00 2026-05-14T20:33:14+00:00

My MySQL tables structure is like this. USER int id varchar username FRIEND_LIST int

  • 0

My MySQL tables structure is like this.

USER
int id
varchar username

FRIEND_LIST
int user_id
int friend_id

For each friend relationship I insert 2 records in FRIEND_LIST.
If user 1 is friend of user 2 then the next rows are inserted into FRIEND_LIST

1,2
2,1

I want to get the friends and friends of friends of an specific user.

The select should return columns a, b, c.

a: user_id
b: friend_id
c: username (username of friend_id )

If 1 is friend of 2 and 3.
2 is friend of 3, 4 and 5
3 is friend of 5,6,7

Then the query to get 1’s friends and friends of friends should return:

1 2 two
1 3 three
2 1 one
2 3 three
2 4 four
2 5 five
3 1 one
3 5 five
3 6 six
3 7 seven

Can I get this rows with a single query?

UPDATE ANSWER: I modified DVK’s answer a little bit and this is the query that returns what I was looking for.

SELECT friends.user_id, friends.friend_id, username

FROM
      FRIEND_LIST friends, USER

WHERE
      CAT_USER.id = friends.friend_id
AND
      friends.user_id = 1

UNION

SELECT
        fof.user_id, fof.friend_id, username
FROM
        FRIEND_LIST friends, FRIEND_LIST fof, USER
WHERE
        USER.id = fof.friend_id
 AND
        friends.friend_id = fof.user_id
 AND
        friends.user_id = 1;
  • 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-14T20:33:16+00:00Added an answer on May 14, 2026 at 8:33 pm

    This is less efficient but readable:

    SELECT friends.user_id, friends.friend_id, username 
    FROM FRIEND_LIST friends, USER
    WHERE USER.id           = friends.friend_id
     AND  USER.id = 1
    UNION
    
    SELECT USER.user_id, fof.friend_id, username 
    FROM FRIEND_LIST friends, FRIEND_LIST fof, USER
    WHERE USER.id           = fof.friend_id
     AND  friends.friend_id = fof.user_id
     AND  USER.id = 1
    

    OR

    SELECT user_id, f_fof.friend_id, username 
    FROM USER, (
        SELECT f.user_id, f.friend_id
        FROM   FRIEND_LIST f
        WHERE  user_id = 1
        UNION
        SELECT f.user_id, fof.friend_id
        FROM   FRIEND_LIST f, FRIEND_LIST fof
        WHERE  user_id = 1
         AND   f.friend_id = fof.user_id
    ) as f_fof
    WHERE USER.id           = f_fof.friend_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My MySQL tables structure is like this. USER uid FRIENDS fuid,fuid2,fapproved For each friend
I have a mysql table structure like that: id int primary key name varchar
I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have two tables on MySQL, with this structure: Users: id | name Accounts:
I'm retrieving data from mysql like this structure => echo <table id='postI . $chat_row['id']
So I have a database that has a structure something like this: [user_table] user_id,
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have a MySQL table with a structure like the following: I'm looking for
I have a mysql database with a table structure like below : Table Name

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.