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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:21:47+00:00 2026-05-30T18:21:47+00:00

I’m making a web app that uses geolocation, and I’m whipping up a nearby

  • 0

I’m making a web app that uses geolocation, and I’m whipping up a “nearby places” view. It’s pretty simple logic: it shows the closest 5 places that are stored in the database. Perfect.

The trick is that I want it to return the closest 5 places OR all of the places within two miles, whichever is greater. In other words, I want the user to be able to see at least all the places within two miles, but if there aren’t 5 places in within that radius, I want them to show the nearest 5.

Let’s use these for sample data sets. Set 1:

| id | name                 | distance  |
+----+----------------------+-----------+
| 3  | Earl of Sandwich     | 0.3       |
| 4  | Nails 'n More        | 0.8       |
| 22 | City Hotel           | 1.7       |
| 5  | Mighty Medicine      | 2.1       |
| 25 | Wonder Wings         | 2.5       |
| 6  | Jean Warehouse       | 2.7       |
| 9  | Ship Safe & Speedy   | 2.9       |
| 2  | Bagel Bonus          | 4.1       |
+----+----------------------+-----------+

Set 2:

| id | name                 | distance  |
+----+----------------------+-----------+
| 3  | Earl of Sandwich     | 0.1       |
| 4  | Nails 'n More        | 0.2       |
| 5  | Mighty Medicine      | 0.5       |
| 6  | Jean Warehouse       | 0.7       |
| 9  | Ship Safe & Speedy   | 0.9       |
| 2  | Bagel Bonus          | 1.2       |
| 22 | City Hotel           | 1.7       |
| 25 | Wonder Wings         | 2.1       |
+----+----------------------+-----------+

In the first set, I’d want to return rows 3, 4, 22, 5, and 25. In the second set, I’d want to show 3, 4, 5, 6, 9, 2, and 22.

I know I could just always limit the query to, say, 100 places, and go through the result set in PHP to filter… but I’m wondering if there’s a more efficient way to do it right in SQL.

  • 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-30T18:21:48+00:00Added an answer on May 30, 2026 at 6:21 pm

    In summary, the way to do this is to run both the queries and take a UNION of the sets. That’s it. There really is very little performance loss doing this because the first set (that can produce >5 rows) is already required if there really are more than 5 rows in the result.

    In detail – original answer below

    For illustration, instead of using 2 data sets, I just use 2 columns in this same sample table, for which the query is shown further below:

    drop table if exists tbl1;
    create table tbl1 (
    id int,
    name varchar(100),
    distance1 float,
    distance2 float
    );
    insert tbl1 values
    ( 3  , 'Earl of Sandwich   ', 0.3, 0.1),
    ( 4  , 'Nails ''n More     ', 0.8, 0.2),
    ( 22 , 'City Hotel         ', 1.7, 1.7),
    ( 5  , 'Mighty Medicine    ', 2.1, 0.5),
    ( 25 , 'Wonder Wings       ', 2.5, 2.1),
    ( 6  , 'Jean Warehouse     ', 2.7, 0.7),
    ( 9  , 'Ship Safe & Speedy ', 2.9, 0.9),
    ( 2  , 'Bagel Bonus        ', 4.1, 1.2);
    

    And the queries and results:

    /* query 1 */
    select id, name, distance1
    from (
        select *
        from tbl1
        where distance1 <= 2.0
        order by distance1) a
    union
    select id, name, distance1
    from (
        select *
         from tbl1
        order by distance1
         limit 5) b;
    
    /* result 1 */
    id;name;distance1
    3;Earl of Sandwich   ;0.3
    4;Nails 'n More     ;0.8
    22;City Hotel         ;1.7
    5;Mighty Medicine    ;2.1
    25;Wonder Wings       ;2.5
    
    /* query 2 */
    select id, name, distance2
    from (
        select *
        from tbl1
        where distance2 <= 2.0
        order by distance2) a
    union
    select id, name, distance2
    from (
        select *
         from tbl1
        order by distance2
         limit 5) b;
    
    /* result 2 */
    id;name;distance2
    3;Earl of Sandwich   ;0.1
    4;Nails 'n More     ;0.2
    5;Mighty Medicine    ;0.5
    6;Jean Warehouse     ;0.7
    9;Ship Safe & Speedy ;0.9
    2;Bagel Bonus        ;1.2
    22;City Hotel         ;1.7
    

    The performance of this query is as good as it gets.
    The first UNION part picks out the ones <2km. This is necessary since you want all the matches.
    The next part selects the top 5 and assuming you have an index, this is trivial to collect.
    The combination (UNION) of both parts very quick.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.