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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:49:09+00:00 2026-05-11T23:49:09+00:00

I basically have three tables, posts, images and postimages (this simply contains the ids

  • 0

I basically have three tables, posts, images and postimages (this simply contains the ids of both the other tables and allows more than one image in each post and each image to be reused in multiple posts if necessary).

I’m trying to list the titles of all the posts, along with a single, small image from each (if one exists)

I can do it with the following:

    $sql ="SELECT * FROM posts p, images im, postimages pi WHERE
    AND pi.post_id = p.id
    AND pi.image_id = im.image_id
    ORDER BY created_at LIMIT 10";

But obviously, if there is more than one image, the record is displayed twice.

I know I could probably do it with a subquery, but that seems horribly inefficient. Is there a more elegant way around this?

Any advice would be appreciated. Thanks.

  • 1 1 Answer
  • 2 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-11T23:49:09+00:00Added an answer on May 11, 2026 at 11:49 pm
    select
        *
    from
        posts p
        left outer join (select post_id, max(image_id) as image_id 
            from postimages group by post_id) pi on
            p.id = pi.post_id
        left outer join images im on
            pi.image_id = im.image_id
    

    You can do the subquery so that it only has to be executed once, not per row. This way, it’s used as a subquery for an entire table, and then joined to the posts. Obviously, this can be MIN(image_id) if you want to take the first image. Really, whatever you prefer.

    Edit: Made it be a left outer join to capture even those images that don’t exist in the posts. This will return null for the image in the case that it happens to be non-existant.

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

Sidebar

Related Questions

I'm a beginner at this. Basically I have a form that has the correct
I have three tables that I would like to query: Streams, Entries, and FieldInstances.
Suppose I have three tables: user , group and xref , a table that
I basically have this web page where you can narrow your search results by
I need to duplicate several tables in a single database. Basically we have an
I have been thinking and learning a lot about forms lately trying to add
I am using the Specification pattern, and have a working implementation (taken from the
I am trying to use sql such as this: SELECT t.*, t2.* FROM templates
I'm revising my question from a few days ago. Rewrote my query. Can someone
We're designing an Android app that has a lot of data (customers, products, orders...),

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.