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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:28:48+00:00 2026-06-17T15:28:48+00:00

So, I have a database in which routes are saved (i.e. start points and

  • 0

So, I have a database in which “routes” are saved (i.e. start points and end points, in the Latitude/Longitude format ‘StartLat’, ‘StartLng’ and ‘EndLat’, my’EndLng’). Currently, people can search for these routes by either their start point or their end point (e.g. “Find and routes where the start point is within 5 miles of my LatLng position.”). They can look them up by either their start point or their end point (e.g. “find start points within range of this marker”, or “find end points within range of this marker”).

I currently have a SQL lookup based on the Haversine formula, and this works very well for me.

What I want to be able to do is join two queries together in a single SQL string to be able to “find all routes where the start point is within ‘x’ miles of “Place A” and the end point is within ‘x’ miles of “Place B”.

I have a feeling that the most elegant way of doing this is to create a join on my existing SQL string, but I can’t for the life of me work out how!

My current SQL string looks like this:

SELECT ID, RouteCode, (3959 * acos(cos(radians('51.5073346')) * cos(radians( EndLat )) * cos(radians( EndLng ) - radians('-0.1276831')) + sin(radians('51.5073346')) * sin(radians( EndLat )))) AS distance FROM markers HAVING distance < 15

(So, in this example above, users would be searching for all routes where the End Point is within 15 miles of the co-ordinates 51.5073346, -0.1276831. If they had wanted to find routes where the Start Point was within 15 miles of their LatLng, then EndLat and EndLng would be replaced by StartLat and StartLng).

Does anyone have a great enough understanding of SQL commands to be able to help me work out the SQL string that I need?

  • 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-17T15:28:49+00:00Added an answer on June 17, 2026 at 3:28 pm

    You don’t need a join; you simply need to add a second condition to your query:

    SELECT ID, 
           RouteCode, 
           (
               3959 * acos(cos(radians('51.5073346')) * cos(radians( EndLat )) * 
               cos(radians( EndLng ) - radians('-0.1276831')) + 
               sin(radians('51.5073346')) *      
               sin(radians( EndLat )))
           ) AS end_distance,
           (
               [similar calculation for start here]
           ) AS start_distance
        FROM markers 
        HAVING start_distance < 15 and end_distance < 15
    

    Conceptually, a join is for adding data from another table. You simply want to impose another requirement within the same table.

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

Sidebar

Related Questions

I have an application which creates page routes from a database. My whole site
I have a database which, simiplified, is as follows : ID | SecondID |
I have a database which is going to have UMT timestamps in standard sql
I have a database which contains the link to audio files. I am trying
I have a database which has a table recording when a vehicle was last
i have a database which contains an arabic tables , i can read it
I have a database which I regularly need to import large amounts of data
I have a database which contains picture data stored as a binary blob. The
I have a database which stores (among other things), the following pieces of information:
I have a database which needs to store year ranges (such as lifespan) which

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.