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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:16:33+00:00 2026-06-09T02:16:33+00:00

Afternoon, I am having some difficulty getting my head around this problem. I have

  • 0

Afternoon,

I am having some difficulty getting my head around this problem. I have a MySQL table with a list of UK Post Codes and their longitude and latitude values. I want to be able to carry out a search on the table that will find the closest postcode to the a given long/lat pair.

The query I have been attempting to use is:

"SELECT id, outcode AS thecode, @la := MATCH(lat) AGAINST(?) AS score_lat, @ln := MATCH(lng) AGAINST(?) AS score_lng, @la + @ln AS score_total FROM postcodes ORDER BY score_total DESC LIMIT 10

This however just returns what appears to be random postcodes, for example with Lat: 55.775549 and Long: -4.047556

Array
(
[0] => Array
    (
        [id] => 929
        [thecode] => FK14
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[1] => Array
    (
        [id] => 2785
        [thecode] => UB3
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[2] => Array
    (
        [id] => 993
        [thecode] => G70
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[3] => Array
    (
        [id] => 2849
        [thecode] => WC2B
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[4] => Array
    (
        [id] => 1057
        [thecode] => GU29
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[5] => Array
    (
        [id] => 2913
        [thecode] => WS13
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[6] => Array
    (
        [id] => 1121
        [thecode] => HP20
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[7] => Array
    (
        [id] => 1185
        [thecode] => IG6
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[8] => Array
    (
        [id] => 1249
        [thecode] => IV25
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )

[9] => Array
    (
        [id] => 1313
        [thecode] => KA8
        [score_lat] => 0
        [score_lng] => 0
        [score_total] => 0
    )
)

The schema of the database is:

CREATE TABLE `postcodes` (
  `id` int(11) NOT NULL auto_increment,
  `outcode` varchar(4) NOT NULL,
  `lat` varchar(20) NOT NULL,
  `lng` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`),
  FULLTEXT KEY `lat` (`lat`),
  FULLTEXT KEY `lng` (`lng`)
) ENGINE=MyISAM AUTO_INCREMENT=2975 DEFAULT CHARSET=latin1 AUTO_INCREMENT=2975 ;

I hope someone can help! If you need any more information please just ask…

Thanks,

tip2tail

  • 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-09T02:16:35+00:00Added an answer on June 9, 2026 at 2:16 am

    The MySQL MATCH() function is used for full-text searches to “match” on character strings. (So it’s not surprising that its returning a value of zero.)

    If by “closest” you mean you want to calculate the distance (as measured ‘as the crow flies’), between two points on a map, with coordinates given in (decimal degrees) latitude and longitude, you’d really need to use a great circle distance (GCD) calculation.

    http://en.wikipedia.org/wiki/Great-circle_distance

    You can skip all those gory details, and just make use of my implementation. Below is an excerpt from the SELECT list of one of my SQL statements, this expression calculates the distance (in miles) between two points…

         , ACOS(
              COS(RADIANS( d2.latitude ))
            * COS(RADIANS( d1.latitude ))
            * COS(RADIANS( d2.longitude ) - RADIANS( d1.longitude ))
            + SIN(RADIANS( d2.latitude ))
            * SIN(RADIANS( d1.latitude ))
               )*3958.82 AS distance_miles
    

    In this example, d1 represents the origin point, and d2 represents the destination point. The latitude and longitude are supplied as DECIMAL values.

    With a single “known” point for d1, I can order by this expression, to get the “closest” d2 first. (For multiple origin points, I can order by d1.id, then by this expression to get the closest d2 first for each d1. But enough about my problem…


    I copied the query from your question and modified it (below). Basically, I removed the “score” columns, and replaced it with an expression that does a distance calculation:

    SELECT id
         , outcode AS thecode
         , ACOS(
               COS(RADIANS( d2.latitude ))
             * COS(RADIANS( @d1_latitude ))
             * COS(RADIANS( d2.longitude ) - RADIANS( @d1_longitude ))
             + SIN(RADIANS( d2.latitude ))
             * SIN(RADIANS( @d1_latitude ))
               )*3958.82 AS distance_miles
      FROM postcodes d2
      JOIN (SELECT @d1_latitude := ?, @d1_longitude := ?) v
     ORDER BY distance_miles LIMIT 10
    

    In this case the @d1_ variables (assigned from bind variables) are the latitude and longitude of your “known” point. For each row in your postcodes table (which I aliased as d2 for convenience), this expression calculates the distance between the lat/long in the table and the “known” point.

    NOTE: the inline view aliased as v is just there so you can bind the latitude just once, and assign the values to user variables that can be referenced. That inline view can be omitted, and you can see where you would need to bind the latitude twice.

    NOTE: this calculates distance in “miles”. You can easily get distance in kilometers (km) by substituting a different value in place of the 3958.82 constant.

    NOTE: It’s not necessary to return the distance; you could just put that expression in the ORDER BY clause if you’re only interested in returning the 10 closest in order by distance, e.g.

    SELECT id
         , outcode AS thecode
      FROM postcodes d2
      JOIN (SELECT @d1_latitude := ?, @d1_longitude := ?) v
     ORDER
        BY ACOS(
               COS(RADIANS( d2.latitude ))
             * COS(RADIANS( @d1_latitude ))
             * COS(RADIANS( d2.longitude ) - RADIANS( @d1_longitude ))
             + SIN(RADIANS( d2.latitude ))
             * SIN(RADIANS( @d1_latitude ))
               )*3958.82 AS distance_miles
     LIMIT 10
    

    Please let me know if you are looking for something other than distance between two points, because in that case, this answer is really of no help to you.

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

Sidebar

Related Questions

Good afternoon, I'm having serious troubles getting my database back up and running. I
Good afternoon, I'm having a problem with a section of code I'm working on
Afternoon all. I have a drop down list in my view <div class=editor-label> @Html.LabelFor(model
I've been trying to fix a problem i've been having all afternoon and was
Afternoon All, I have a simple gridview with list items items that have been
Afternoon. Im struggling with this situation. I have a very simple tabbed content inside
Afternoon, say I have a table like so <table id=foo> <tr><td><!-- label --><!-- textbox
Afternoon all I have a lovely webservice call that brings back a list, we'll
Afternoon All I have some data - var searchwithin = [ {id:clearwithin, type:button}, {id:search_radius,
I have a mysql table with users and their weekly calendar. Every user can

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.