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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:57:40+00:00 2026-05-11T20:57:40+00:00

User has pets , pets may have pictures One picture may be set as

  • 0

User has pets, pets may have pictures One picture may be set as mainpicture (mainpic=0 or mainpic=1)

I need to get all the pet data from the pets table and I also need the picture id where mainpic=1 or first picture or null if he has no picture in the picture table. I need to get the pet data even if it has no picture at all.

Currently I use 2 queries to get this done:
SELECT * FROM pets WHERE pets.userId=’$userId’ ORDER BY pets.created ASC LIMIT 5
…
for each result: SELECT id FROM pictures WHERE petId = ‘$petId’ ORDER BY mainpic DESC LIMIT 1

I need this optimized and in one query.

Thanks,
Hamlet

  • 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-11T20:57:41+00:00Added an answer on May 11, 2026 at 8:57 pm

    use an inner join to omit unmatched records

    select * 
    from pets inner join pictures 
    on pets.id = petId and mainpic=1
    where userId=? 
    order by pets.created ASC limit 5
    

    if you only want null in the picture related fields but want the pet info (your Q was a little unclear) then use a left join

    select * 
    from pets left join pictures 
    on pets.id = petId and mainpic=1
    where userId=? 
    order by pets.created ASC limit 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Setup I have two tables, USERS and PETS . each user has an
A user has sent me some information that they posted to one of my
After a user has logged in using Facebook I need to access their Facebook
If a user has turned off location services (either for all apps or just
One user has said to me Applying incremental db backups is tedious, and a
A user has many tasks. In the a tasks controller view, need to show
User has html-form, then user fills all field and data transferred to PHP script.
If a user has an alternative dialer installed (i.e. more than one dialer on
If a user has >1 Twitter accounts set up of their device, I would
Once a user has tweeted something, I need to differentiate which twitter account he

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.