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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:56:59+00:00 2026-05-26T17:56:59+00:00

First time posting, I will try to adhere to best practices. I’m trying to

  • 0

First time posting, I will try to adhere to best practices.

I’m trying to construct a MySQL query that measures a user’s location (represented in the query as $Lat and $Lng for latitude and longtitude, respectively). The query should return the 50 stations in closest proximity to the user.

The problem is that the data in my table contains the location of each station entrance per station and I only need the closest station entrance per station!

This is my query:

SELECT id, lat, lng, station_name, routes,
  ( 3959 * acos( cos( radians($Lat) )
  * cos( radians( lat ) ) * cos( radians( lng ) -
  radians($Lng) ) + sin( radians($Lat) ) * sin( radians( lat ) ) ) )  AS distance
FROM subway_stations ORDER BY distance LIMIT 0 , 50;

The Above MySQL query accomplishes the following:

  1. Selects the data from id, lat, lng, station_name and routes
  2. Measures lat and lng of each station with that of the user – storing that data as ‘distance’
  3. Returns the 50 nearest results

I need to group these results together so that only one record per station is returned, the one with the lowest value in the distance column, which is the station entrance closest to the user.

I’ve tried using GROUP BY but I seem to be implementing it incorrectly as the results returned were not the desired ones.

  • 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-26T17:56:59+00:00Added an answer on May 26, 2026 at 5:56 pm

    It’s hard to know without setting up a sample database, but I suspect that you want to make the following changes:

    1. Add a group by station_name, id, lat, lng to the end of your query

    2. All select fields which are not a part of the group by clause need an aggregating function. Most obviously, distance should needs a min(...)

    3. I’m not sure if the order and limit clause can be used. If you have problems, take them out (temporarily) and get this much working. For my convenience, call this query Q1.

    4. If the order by and limit clause cannot be used (experiment and see), then you want a nested query. That will look something like select * from (...Q1...) t1 ORDER BY distance LIMIT 0,50. The t1 assigned a temporary name to the inner select, not actually used in this query, but required for syntax.

    5. As an optimization, you may want to add a having clause to the inner Q1 query. That is, add HAVING distance<XXXXX, if you know a bounding distance.

    Note that steps 3 — 5 may not be needed, I’m not sure about step 3.

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

Sidebar

Related Questions

First time posting here, will try to be succinct. This is a classic 'can't
first time posting in StackOverflow. :D I need my software to add a couple
first time posting here. I'm using a loop to create 3 buttons, but my
this is my first time posting here, I have a question which I have
use this website a lot but first time posting. My program creates a number
first time SO user :) I know that I can format a number like
First time trying to deal with users logging in with ASP.net and haven't had
First time posting on stack overflow. I've spent hours scouring over many Google searches
first time posting here. I'm a beginner in jquery and i ran into some
first time posting here, Googled for a few hours and I couldn't find anything.

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.