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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:24:50+00:00 2026-05-13T06:24:50+00:00

I have two tables. One with information about properties. The other stores 3 images

  • 0

I have two tables. One with information about properties. The other stores 3 images for each property. Of these three images – one is marked as being the “main” image.

So I have:

Properties:
p_id    name
1   villa a
2   villa b
3   villa c
4   villa d

and

    Images
i_id p_id main
1    1     0
2    1     0
3    1     1
4    2     0
5    2     1
6    2     0

I need to produce a query which returns all of the properties with the id of their main image. e.g.

p_id  name    i_id
1     villa a  3
2     villa b  5

I know this will involve using LIMIT 1 and a join, but not sure where to start, I have already attempted doing this by using a subquery but felt it must be less complicated than what I was doing….

* HOW DO I *
Make it so it orders the query by “main” selecting the top 1 (i.e. so if main is not set it will still select an image) ?

  • 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-13T06:24:51+00:00Added an answer on May 13, 2026 at 6:24 am

    Here’s one way:

    select      *
    from        properties p
    left join   images i 
    on          p.p_id = i.p_id 
    and         i.main = 1
    

    The left join will return a NULL image if no main image is found.

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

Sidebar

Related Questions

i have two tables one is called addons and holds information about different addons,
I have two Postgres databases. In one I have two tables, each with about
hope this makes sense. I have merged together two tables one containing information about
I have two tables, one table has some information in each row along with
I have two tables The First one contains information about staff who take courses
I have two tables. One table contains information about Assets, another Table about their
I have two tables for storing information about a user. One is for authentication,
I have two tables one called fs_note the other called dumy_fs_note I created after
I have two tables : one table it matchs and the other is teams.
I have two tables, one that has 450 rows, the other is a Sort

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.