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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:13:39+00:00 2026-06-04T21:13:39+00:00

I wish to query 3 different tables. My approach would be getting the the

  • 0

I wish to query 3 different tables.
enter image description here

My approach would be getting the the friends from FRIENDS TABLE. So, the output would be:
enter image description here

I tried having this solution

SELECT users.firstname, users.lastname, 
users.screenname, profile.profile_pic, friends.friend_id
FROM website.users users
INNER JOIN website.friends friends ON (users.user_id = friends.friend_id)
LEFT  JOIN profile ON (friends.friend_id = profile.user_id)
WHERE (friends.user_id = "12052822" or friends.friend_id = "12052822")

Problem is when it matches one record, it outputs the the column “friend.user_id”. That’s not what I want to do! If the user is logged in, a query will fetch all his friends… How can I do that in an easier and better way?

  • 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-04T21:13:40+00:00Added an answer on June 4, 2026 at 9:13 pm

    The id and user_id columns in the users table are at least confusing? Why two ‘identifier’ columns? Which of the two joins with friend.user_id? Is it the same column that joins with friend.friend_id? And is the objective to display all the names where the friends.user_id is the user who just logged in, or all the names where the friends.friend_id is the user who just logged in, or both?

    Until further clarification is forthcoming, I will assume the result set should include both lists of friends: the people who have been friended by the user and the people who have friended the user. I will also assume that the users.user_id column is the joining column.

    We can write this easily as a UNION, noting that a UNION will eliminate duplicate entries for the case where the user who logged in has friended a user who has also friended the user who logged in (so there are two entries in the friends table with the same pair of values in user_id and friend_id.

    Going in for TDQD (Test Driven Query Design), we can write (assuming the newly logged in user is user_id = 12052822):

    Users friended by user 12052822

    SELECT u.user_id, u.firstname, u.lastname, u.screenname, p.profile_pic
      FROM website.friends AS f
      JOIN website.users   AS u ON f.friend_id = u.user_id
      JOIN website.profile AS p ON p.user_id   = u.user_id
     WHERE f.user_id = 12052822;
    

    Users who have friended user 12052822

    SELECT u.user_id, u.firstname, u.lastname, u.screenname, p.profile_pic
      FROM website.friends AS f
      JOIN website.users   AS u ON f.user_id = u.user_id
      JOIN website.profile AS p ON p.user_id = u.user_id
     WHERE f.friend_id = 12052822;
    

    Both sets of users

    SELECT u.user_id, u.firstname, u.lastname, u.screenname, p.profile_pic
      FROM website.friends AS f
      JOIN website.users   AS u ON f.friend_id = u.user_id
      JOIN website.profile AS p ON p.user_id   = u.user_id
     WHERE f.user_id = 12052822;
    UNION
    SELECT u.user_id, u.firstname, u.lastname, u.screenname, p.profile_pic
      FROM website.friends AS f
      JOIN website.users   AS u ON f.user_id = u.user_id
      JOIN website.profile AS p ON p.user_id = u.user_id
     WHERE f.friend_id = 12052822;
    

    You can test and debug each of the component queries separately, and then combine them to create the final answer.

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

Sidebar

Related Questions

Really wish i was God, then I would understand what this means: The element
I have this query to count views in a table, by date. It obviously
After making a SQL query to Table Post I wish to hydrate the $result
I am receiving a string via query string and I wish to add this
I have a query like this: select data.* from ((select table1.col1 as a) union
This is continuing on from a query that was solved previously via stackoverflow (
<?php function f__table ($table,$cols,$order,$desc) { $comma=implode(,,$cols); $query = 'select '.$comma.' from '.$table.' order by
So I am doing this query from PHP, and here listerally the exact query
I have a mysql query: $result = mysql_query(SELECT * FROM $table WHERE cat =
I wish to use a jQuery .load(oracle apex url) query. I have a main

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.