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

  • Home
  • SEARCH
  • 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 948461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:14:58+00:00 2026-05-15T23:14:58+00:00

I have those tables Node (#id, route_id, lat, lng) Route(#id) And I have four

  • 0

I have those tables

Node (#id, route_id, lat, lng)

Route(#id)

And I have four input values : $lat1, $lng1, $lat2, $lng2, which represent 2 GPS coordinates GPS1 and GPS2.
I also have a distance function that I added to the database : distance( lat1, lng1, lat2, lng2)

What I am looking for is the request that will give me the closest point to GPS1, and the closest point to GPS2 for each route.
So I guess it should start like :

SELECT r.id, n1.lat, n1.lng, n2.lat, n2.lng
FROM Node n1, Node n2
JOIN Route r ON n1.route_id = r.id AND n2.route_id = r.id

And I tried to add

WHERE distance(n1.lat,n1.lng,$lat1,$lng1) = MIN (distance(n1.lat,n1.lng,$lat1,$lng1))
AND distance(n2.lat,n2.lng,$lat2,$lng2) = MIN (distance(n2.lat,n2.lng,$lat2,$lng2))

But it throws this error :

"misuse of aggregate function MIN()"

I also thought I could use

SELECT r.id, n1.lat, n1.lng, n2.lat, n2.lng, distance(n1.lat,n1.lng,$lat1,$lng1) AS d1, distance(n2.lat,n2.lng,$lat2,$lng2) AS d2
FROM Node n1, Node n2
JOIN Route r ON n1.route_id = r.id AND n2.route_id = r.id
GROUP BY r.id

And to sort it by d1 for the n1.lat, n1.lng, d1 columns and by d2 for the n2.lat, n2.lng, d2 columns but I don’t know how to do that.

Any idea ?

  • 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-15T23:14:59+00:00Added an answer on May 15, 2026 at 11:14 pm

    You need to perform a correlated query:

    SELECT r.id, n1.lat, n1.lng, n2.lat, n2.lng
    FROM Node n1, Node n2
    JOIN Route r ON n1.route_id = r.id AND n2.route_id = r.id
    WHERE distance(n1.lat,n1.lng,$lat1,$lng1) = (
                                                 SELECT MIN (distance(lat,lng,$lat1,$lng1))
                                                 FROM Node c_n
                                                 WHERE c_n.nid = n1.nid)
    AND distance(n2.lat,n2.lng,$lat2,$lng2) = (
                                                 SELECT MIN (distance(lat,lng,$lat1,$lng1))
                                                 FROM Node c_n
                                                 WHERE c_n.nid = n2.nid)
    

    (At this conjecture I am unable to test this, please forgive me if this is not exact, but it should be close. I will check up on this later.)

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

Sidebar

Related Questions

I have those two tables: client: id (int) #PK name (varchar) client_category: id (int)
In my application I have small tables(files) with unique keys (most of those have
I have two tables, one containing cities and one containing countries. Those are bi-directional
I've 3 different database tables that have the same 5 fields but those does
I have these tables and values: Table1 ------------------------ ID | Value ------------------------ 2 |
I have 2 tables MachineGroups and Machines. Machine Groups has values: MachinegroupID MachineGroupName MAchineGroupDesc
I have these tables: comments uid id pid pages pid user users id rank
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 these tables: Idea and poll (and others) which are types of what
I have these tables: table_a user_id article_id created_at articles user_id created_at ... I need

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.