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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:15:44+00:00 2026-05-26T10:15:44+00:00

i have two tables. a user table and a photo table. IF a user

  • 0

i have two tables. a user table and a photo table. IF a user uploads photo’s, they have the option of specifying only one as a ‘primary’ profile photo (enforced on the front-end).

tbl_users
---------
user_id
username

tbl_photos
---------
photo_id
user_id
is_primary (bit)
file_name

when i’m querying a list of users on a user search, for example, i want their chosen primary photo to display. however, if there are no linked records in the photo table or there are, but none are marked primary, I still want to return the users record and perhaps NULL or some other value for the file_name.

how would I accomplish this?

ps. i’m currently doing this through two separate calls to the database, but want this done in one call for efficiency.

  • 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-26T10:15:45+00:00Added an answer on May 26, 2026 at 10:15 am

    Depending on what DB you use this works (userid2query being the id of the user you want to retrieve):

    SELECT u.user_id, u.username, p.photo_id, p.file_name 
    from tbl_users u
    left outer join tbl_photos p on u.user_id = p.user_id and p.is_primary = 1
    where u.user_id = userid2query
    

    It returns the user information and the primary photo information – IF no primary photo or no photo at all it stills returns the user information but returns NULL for the photo information columns.

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

Sidebar

Related Questions

I have a simple mySQL problem-- I have two tables, one is a user's
so I have two tables. They are pictured below. I have a master table
I have two tables users registered_members I want to confirm values from user table
I have two tables, in which one user can have several contacts: User(UserId, UserName)
I have two tables User & User Log . User log table basically logs
I have two tables User and UserRole which are they connected using a link
Hi ok I have two tables one called Login and one called User there
I have two tables, one user_info : table user_info { user_id ; fname; laname;
I have two tables, 'photos' and 'videos'. Together they are almost identical, except one
I have two tables user (one) and transaction (many) and I need to get

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.