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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:32:05+00:00 2026-06-09T03:32:05+00:00

I have table places place_id | city | country_code | zipcode | lat |

  • 0

I have table places

place_id | city | country_code | zipcode | lat | lon

Now I want to show places that are within 25 miles of place A. Place A has place_id 1.

SELECT * 
FROM `places` 
WHERE ( 3959 * acos( cos( radians((SELECT lat FROM places WHERE place_id=1)) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians((SELECT lon FROM places WHERE place_id=1)) ) + sin( radians((SELECT lat FROM places WHERE place_id=1)) ) * sin( radians( lat ) ) ) ) < 25;

This works ok, but there are two the same subqueries

SELECT lat FROM places WHERE place_id=1

Is it possible to optimize this query to not have two the same subqueries but make them into 1?

  • 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-06-09T03:32:08+00:00Added an answer on June 9, 2026 at 3:32 am

    Your three subqueries will execute for every row in the outer table.

    What you can do is convert those subqueries to a single JOIN which will only execute once for the entire query in order to find the latitude and longitude of place_id 1:

    SELECT a.* 
    FROM   places a
    JOIN   (SELECT lat, lon FROM places WHERE place_id = 1) b ON
           (3959 * acos( cos( radians(b.lat) ) * cos( radians( a.lat ) ) * cos( radians( a.lon ) - radians(b.lon) ) + sin( radians(b.lat) ) * sin( radians( a.lat ) ) ) ) < 25;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that's generated by a normal PHP loop. What I want
I have these tables places(place_id, place_name) places_criteria(place_id, criterion_id) criteria(criterion_id, criterion_name) places_criteria have foreign keys
i have a table e.g: CREATE TABLE places (id UNIQUE, name, latitude, longitude) i
I want to place a table on my page. I have two tables in
I have an NSFetchedResultsController displaying places in a table view, but when I update
I have two databases at two different places, both databases are same with table
I have table with around 70 000 rows. There is 6000 rows that i
We have a website that contains a database of places. For each place our
I am designing a data model for tourism-site. I have a table for places
Let's suppose I have this MySQL table: id place_id area_mq n -- -------- -------

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.