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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:03:00+00:00 2026-06-11T21:03:00+00:00

I have been hunting around for a while now on this, but nothing is

  • 0

I have been hunting around for a while now on this, but nothing is really coming up that does exactly what i need.

I am searching a table that contains entries with longitude and latitude values. This will return a set of results. There are also entries that don’t have long/lat values that I need to return based on some date values.

I know that the two generally wont produce duplicate results, but I am interested nonetheless to know how to do this kind of query.

Q1:

"SELECT id, from_date, to_date, event_title, event_longitude, event_latitude, 
(SQRT( POW( 69.1 * ( event_latitude - 51.4999501637 ) , 2 ) + 
POW( 69.1 * ( -0.195226 - event_longitude ) * COS( event_latitude / 57.3 ) , 2 ) ) 
* 1609.344) AS distance, (SQRT( POW( 69.1 * ( event_latitude - 51.4999501637 ) , 2 ) 
+ POW( 69.1 * ( -0.195226 - event_longitude ) * COS( event_latitude / 57.3 ) , 2 ) ) 
* 1609.344) AS true_distance FROM table1 WHERE id!=0 AND no_place='0' 
AND from_date >= '45645646' AND to_date <= '76647446' HAVING distance <= '5000' 
ORDER BY distance ASC LIMIT 50"

this will return the entries matching the long/lat range and ignores any entry marked no_place=0. This uses Haversine to do the long/lat calcs.

the second query needs to pull the entries that have no long/lat but match the date range:

Q2:

"SELECT id, from_date, to_date, event_title, event_longitude, event_latitude FROM
table1 WHERE no_place='1' AND from_date >= '45645646' AND to_date <= '76647446' 
LIMIT 50"

I could run two separate queries here, and append
“WHERE ids !in(ids,from,q1)”

to the 2nd query, then use PHP to merge the two results, MySQL must have some cunning method to do this; so the question is this:

How can I do this in just one query, and ignore any ids already found in Q1. I know I have the no_place column which in this scenario will guarantee i have no duplicate rows, but assuming I didn’t have this column, how would i go about adding the

"WHERE ids !in(ids,from,q1)" 

clause? Also I think I will probably need to order this total result with the no_place=1 entries first, then the no_place=0 entries ordered by their distance value.

phew!


update after great answer from “Hope I Helped”

so this seems to work, but it looses the distance values in the result:

(SELECT id, from_date, to_date, event_title, event_longitude, event_latitude, 
(SQRT( POW( 69.1 * ( event_latitude - 51.4999501637 ) , 2 ) + POW( 69.1 * 
( -0.195226 - event_longitude ) * COS( event_latitude / 57.3 ) , 2 ) ) * 1609.344) 
AS distance, (SQRT( POW( 69.1 * ( event_latitude - 51.4999501637 ) , 2 ) + POW( 69.1 
* ( -0.195226 - event_longitude ) * COS( event_latitude / 57.3 ) , 2 ) ) * 1609.344) 
AS true_distance FROM table1 WHERE id!=0 AND no_place='0' 
HAVING distance <= '5000' ORDER BY distance ASC LIMIT 50) UNION(SELECT id, from_date, 
to_date, event_title, event_longitude, event_latitude, 0, 0 FROM table1 WHERE 
no_place='1' LIMIT 50)

this works, but i have no radius limit in the query now…

(SELECT id, from_date, to_date, event_title, event_longitude, event_latitude, 
(SQRT( POW( 69.1 * ( event_latitude - 51.4999501637 ) , 2 ) + 
POW( 69.1 * ( -0.195226 - event_longitude ) * COS( event_latitude / 57.3 ) , 2 ) ) 
* 1609.344) AS distance, (SQRT( POW( 69.1 * ( event_latitude - 51.4999501637 ) , 2 ) 
+ POW( 69.1 * ( -0.195226 - event_longitude ) * COS( event_latitude / 57.3 ) , 2 ) ) 
* 1609.344) AS true_distance FROM table1 WHERE id!=0 AND no_place='0' ORDER BY
distance ASC LIMIT 50) UNION(SELECT id, from_date, to_date, event_title, 
event_longitude, event_latitude, 0, 0 FROM table1 WHERE no_place='1' LIMIT 50)

any ideas?

  • 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-11T21:03:01+00:00Added an answer on June 11, 2026 at 9:03 pm

    Try:

    (SELECT id, from_date, to_date, event_title, event_longitude, event_latitude,
        (SQRT( POW( 69.1 * ( event_latitude - 51.4999501637 ) , 2 ) +
        POW( 69.1 * ( -0.195226 - event_longitude ) *
        COS( event_latitude / 57.3 ) , 2 ) ) * 1609.344) AS distance,
        (SQRT( POW( 69.1 * ( event_latitude - 51.4999501637 ) , 2 ) +
        POW( 69.1 * ( -0.195226 - event_longitude ) *
        COS( event_latitude / 57.3 ) , 2 ) ) * 1609.344) AS true_distance
        FROM table1
        WHERE id!=0 AND no_place='0' AND from_date >= '45645646' AND
        to_date <= '76647446'
        HAVING distance <= '5000'
        ORDER BY distance ASC 
        LIMIT 50)
            UNION
        (SELECT id, from_date, to_date, event_title, event_longitude, event_latitude,
        0, 0
        FROM table1
        WHERE no_place='1' AND from_date >= '45645646' AND to_date <= '76647446'
        LIMIT 50)
    

    By applying a UNION, you’re able to retrieve the results from both sets, without duplicates.

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

Sidebar

Related Questions

I've been hunting for a long time for this, but despite my intuition that
HI all, I have been hunting around for some code that implements various correlation
Surely this is possible? I have been hunting through PyQt tutorials and documentation but
I have been hunting for the answer to this but most seem to point
This one has been haunting me for quite a while now.. I have been
I've been hunting around the net now for a few days trying to figure
Have been googling for a while, but I don't find any page which explains
I've been hunting around and Googling for examples, but I can't see an obvious
I've been hunting around for a solution for this and cant find one, looked
Have been working in Ruby for a while, but usually in the context of

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.