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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:45:29+00:00 2026-05-25T17:45:29+00:00

I have a query that retrieves location entities within a certain radius given a

  • 0

I have a query that retrieves location entities within a certain radius given a latitude and longitude.

I’m now trying to adapt that query to return a list of users within a certain radius, with their most recent location alongside it. My problem is that if I GROUP BY the user id, I can’t then sort the locations to only return the most recent one.

I’ve tried following ypercube’s advice on using a subquery which has got me this far:

SET @mylon = -1.095414;
SET @mylat = 50.79486;

SELECT  u.*, dest.*
FROM    users AS u
JOIN    locations AS dest
ON      dest.id = 
        (
        SELECT  l.id, 3956 * 2 * ASIN(SQRT(POWER(SIN((@mylat - abs(l.latitude)) * pi()/180/2),
                2) + COS(@mylat * pi()/180) * COS(abs(l.latitude) *
                pi()/180) * POWER(SIN((@mylon - l.longitude) *
                pi()/180/2),2))) as distance
        FROM    locations AS l
        HAVING  distance < 0.5
        ORDER BY
                l.created DESC
        LIMIT 1
        )

However, this returns an SQL error as I’ve got more than one field being returned by my subquery.

Any ideas how to fix this? 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-25T17:45:30+00:00Added an answer on May 25, 2026 at 5:45 pm

    First, your subquery must return only an id that should be matched with the dest.id.
    I don’t see you using GROUP BY so you should replace HAVING with WHERE.
    I think you can directly say WHERE (3956 * 2 * ASIN(…….) < 0.5 so that you don’t put that computation in the result and leave only the id

    SET @mylon = -1.095414;
    SET @mylat = 50.79486;
    
    SELECT  u.*, dest.*
    FROM    users AS u
    JOIN    locations AS dest
    ON      dest.id = 
        (
        SELECT  l.id
        FROM    locations AS l
        WHERE (3956 * 2 * ASIN(SQRT(POWER(SIN((@mylat - abs(l.latitude)) * pi()/180/2),
                2) + COS(@mylat * pi()/180) * COS(abs(l.latitude) *
                pi()/180) * POWER(SIN((@mylon - l.longitude) *
                pi()/180/2),2)))) < 0.5
        ORDER BY
                l.created DESC
        LIMIT 1
        )
    

    To return also the computation you may be able to create a temporary table like this

    SET @mylon = -1.095414;
    SET @mylat = 50.79486;
    
    SELECT  u.*, dest.*
    FROM    users AS u
    JOIN    (
        SELECT  l.id, l.created, (3956 * 2 * ASIN(SQRT(POWER(SIN((@mylat - abs(l.latitude)) * pi()/180/2),
            2) + COS(@mylat * pi()/180) * COS(abs(l.latitude) *
            pi()/180) * POWER(SIN((@mylon - l.longitude) *
            pi()/180/2),2)))) as distance
    FROM    locations  ) as location_table_tmp
    ON u.id = location_table_tmp.id
    ORDER BY location_table_tmp.created DESC
    

    I’m not sure if the syntax is Ok, try to tweak a little bit and hope it works

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

Sidebar

Related Questions

I have a query that retrieves the users that are online, and a users
I have an MDX query that retrieves data from an SSAS 2005 cube: SELECT
I have a working query that retrieves the data that I need, but unfortunately
I have a query that retrieves a list of received part shipments from plants.
I have a LINQ query that retrieves rows from a dataset and binds it
I have a query that retrieves the name of each friend a user has
I have the following query that retrieve the number of users per country; SELECT
I have a MySQL query that I use to retrieve random rows from a
I am using in C# MYsql .I have query that works if I run
I have a query that is dynamically built after looking up a field list

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.