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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:55:09+00:00 2026-05-19T00:55:09+00:00

I want to search nearest locations from coordinates (lat/lng) into mysql database, I use

  • 0

I want to search nearest locations from coordinates (lat/lng) into mysql database, I use spherical law of cosines to searching these places :

SELECT onlycoord.id, locations.name, locations.ascii,
    locations.latitude, locations.longitude,
    locations.country, locations.elevation,
    locations.gtopo30, locations.timezone,
    (6371 * ACOS(
        COS( RADIANS( 48.48 ) ) *
        COS( RADIANS( onlycoord.latitude ) ) *
        COS(
            RADIANS( onlycoord.longitude ) -
            RADIANS( 2.20 )
        ) +
        SIN( RADIANS( 48.48 ) ) *
        SIN( RADIANS( onlycoord.latitude ) )
    )) AS distance
FROM onlycoord USE INDEX (coordinate)
    LEFT JOIN locations USE INDEX (id)
    ON (onlycoord.id = locations.id)
WHERE onlycoord.latitude BETWEEN (
    48.48 - ( 5 / 111 )
) AND (
    48.48 + ( 5 / 111 )
) AND onlycoord.longitude BETWEEN (
    2.20 - ( 5 / ABS( COS(
        RADIANS( 48.48 )
    ) * 111 ) )
) AND (
    2.20 + ( 5 / ABS( COS(
        RADIANS( 48.48 ) 
    ) * 111 ) )
)
ORDER BY distance ASC
LIMIT 0, 20

Where 6371 is earth radius (km), 111 (km) is 1° of latitude, cos(latitude) * 111 (km) is 1° of longitude and 5 (km) is search radius.

Problem : I want a minimum of 8 cities found, but 5 kms radius is small but fast for condensed zone (many cities), so if I use a search radius too big for condensed zone, query is slow (many results : order by), but for non-condensed the search radius is too small to find at least 8 cities…

How can I make a recursive query which increases automatically the search radius (x2) if the count of found cities < 8 (using mysql only) ?

Thanks

  • 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-19T00:55:10+00:00Added an answer on May 19, 2026 at 12:55 am

    MySQL doesn’t support recursive queries.

    You have to make new queries depending on the result of the previous query.

    By the way, for spatial queries I’d recommend a spatial database.

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

Sidebar

Related Questions

I want to search for text 'law' in MySQL colum 'content(colume_name)' that has value
I want to search a file in the current directory from which the batch
Hello everyone i want search data from invoices and client by today date I'm
Hello everyone i want search data from invoices and client by today date I'm
I need Yoour help: I want search from table how many users(id_uzy) makes reservation
how to modify this code if i want search JD from var str=KD-S35JWD ..i
I want to search a file from the path. For Example, Path = C:\Newfolder\
I'm searching a value in my MySQL-Database and want to sort the results by
I want to add the functionality to search for the nearest outlet(same like this
I want to search for files containing DOS line endings with grep on Linux.

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.