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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:31:01+00:00 2026-05-20T07:31:01+00:00

I have an images table and a locations table I want to retrieve a

  • 0

I have an images table and a locations table
I want to retrieve a list of images that are the latest images for each location within a certain boundaries.

SELECT * FROM images 
    WHERE location_id IN 
       (SELECT id FROM locations 
            WHERE latitude > 17.954 AND latitude < 52.574 
            AND longitude > -107.392 AND longitude < -64.853)

This is with a nested query, but we could achieve the same with a join.
This works if we want all images for each location, but I would like to get only 1 image per location (the most recent)

Here are the main fields of these tables

  table "images"
    integer  "id"
    text     "image_name"
    text     "caption"
    integer  "location_id"
    datetime "created_at"
    datetime "updated_at"
    integer  "view_count"

  table "locations"
    integer  "id"
    text     "name"
    float    "longitude"
    float    "latitude"
    datetime "created_at"
    datetime "updated_at"
    string   "city"
    string   "address"
    string   "province"
    string   "country"
    string   "post_code"

any idea?

Bonus points if there is a way to do this using rails activerecord API

  • 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-20T07:31:02+00:00Added an answer on May 20, 2026 at 7:31 am

    You will need to make use of aliases and aggregation in a sub-query.

    SELECT * FROM images IMG
        WHERE location_id IN
           (SELECT id FROM locations
               WHERE latitude > 17.954 AND latitude < 52.574
               AND longitude > -107.392 AND longitude < -64.853)
        AND created_at IN
           (SELECT MAX(created_at) FROM images IMG2
                  WHERE IMG2.location_id=IMG.location_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of images in a table like so: <div id=scroll-uploader-photos> <table>
Basically I have a database with two tables, that is, Updates table and Images
I have a database table and that table has 6 rows. What I want
I have a table in SQL Server 2005. alt text http://www.techpint.com/sites/default/files/images/table.JPG I want to
I have a table with images and tags in it (called tbl_images ) image_file
I have 2 ways of adding images to a table and, which is most
I have two tables, content and images (and a ContentImages table for the one
I have the following code that produces a background image in a table cell.
I have a table that stores image name in the table and the file
I am creating a web page, where I have an image that I want

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.