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

The Archive Base Latest Questions

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

I have a table called friends and a table photos. I’m trying to query

  • 0

I have a table called friends and a table photos. I’m trying to query friends photos OR the users who has the friends photos. I think I need to alias an inner join on the friends table but not sure

Friends Table
user_id int
friend_id int 
[other attribute field]

Photos Table
id      int
user_id int
[other attribute field]

For example if I want to query friends photos i’d run

SELECT `photos`.`id` FROM `photos` LEFT JOIN `friends` ON (`friends`.`friend_id` = `photos`.`user_id`) WHERE `friends`.`user_id` = 1

Or if I wanted to query just the users table id do

SELECT `photos`.`id` FROM `photos` WHERE `photos`.`user_id` = 1

The problem is when I try to get both in the same result set like

SELECT  `photos`.`id` 
FROM  `photos` 
LEFT JOIN  `friends` ON (  `friends`.`friend_id` =  `photos`.`user_id` ) 
WHERE  `friends`.`user_id` =1
OR  `photos`.`user_id` =1
LIMIT 0 , 30

Which obviously gets the wrong results because of the left join I get a result for every friend I have

I’m thinking I need to inner join an aliased table for the OR clause or something but can’t figure it out.

  • 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-15T20:45:18+00:00Added an answer on June 15, 2026 at 8:45 pm

    What if just use UNION for two your first queries:

    SELECT `photos`.`id` 
       FROM `photos` 
            LEFT JOIN `friends` ON (`friends`.`friend_id` = `photos`.`user_id`) 
       WHERE `friends`.`user_id` = 1
    union all 
    SELECT `photos`.`id` FROM `photos` WHERE `photos`.`user_id` = 1
    

    If you do not want to use UNION then

    SELECT `photos`.`id` 
       FROM `photos` where `photos`.`user_id`=1
                       or `photos`.`user_id` 
                           in (select `friends`.`friend_id` from `friends`
                                                 where `friends`.`user_id` = 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called USERS that has a foreign key to the table
Hi I have a table called Users and a table called friends, friends table
I have a table called friends which has id and name and a self
I have a table called 'friends', this contains the columns person1 and person2 .
I have table called posts in my DB. Each post has field called social_network
I have a table called user_relationship. which has two foreign keys refering back to
I have table called view results in my db.it has 3 columns.. I want
In my db I a have table called users. In that table I have
I have a table called Friends are its fields are name, phone, address, skype,
I have a data table. Each row of the table has a commandButton called

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.