0

I have two table as shown below:

tbl_properties and tbl_postcodes

It shall list the properites in surrounding 5 kms of the selected postcode/s.

I am trying to get the result with below query but its not getting correct results.

SELECT* FROM (

SELECT p.*, ROUND( 60 * 1.1515 * 1.609344 * DEGREES( ACOS( COS(RADIANS(pc.latitude)) * COS(RADIANS(p.lat)) * COS(RADIANS(pc.longitude - p.lng)) + SIN(RADIANS(pc.latitude)) * SIN(RADIANS(p.lng)) ) ), 2 ) AS distance

FROM properties AS p JOIN postcodes_geo AS pc ON (p.postcode_id = pc.id)

WHERE p.postcode_id IN (792,790,786,12613) ) AS ftbl WHERE distance <= 5

Krishna
  • 27
  • 5

2 Answers2

0

Following is the web service which provides same feature which you require for searching based on postcode.

http://www.geonames.org/export/web-services.html

Here you can pass postcode and radius as parameter and it will return list of zipcodes related to that in XML and JSON format.

for example: http://api.geonames.org/findNearbyPostalCodesJSON?postalcode=2032&country=AU&radius=10&username=demo

V_K
  • 226
  • 1
  • 10
-1

@krishna how about query like

SELECT *
FROM (
    SELECT properties.*
        ,(111.045000 * DEGREES(ACOS(COS(RADIANS(- 33.867487)) * COS(RADIANS(lat)) * COS(RADIANS(151.206990 - lng)) + SIN(RADIANS(- 33.867487)) * SIN(RADIANS(lat))))) AS distance
    FROM `properties`
    WHERE `properties`.`deleted_at` IS NULL
        AND lat BETWEEN - 33.912514
            AND - 33.822460
        AND lng BETWEEN 151.152763
            AND 151.261218
    ) AS properties
WHERE `properties`.`deleted_at` IS NULL

UNION

SELECT *
FROM (
    SELECT properties.*
        ,(111.045000 * DEGREES(ACOS(COS(RADIANS(- 33.914437)) * COS(RADIANS(lat)) * COS(RADIANS(151.241642 - lng)) + SIN(RADIANS(- 33.914437)) * SIN(RADIANS(lat))))) AS distance
    FROM `properties`
    WHERE `properties`.`deleted_at` IS NULL
        AND lat BETWEEN - 33.959464
            AND - 33.869411
        AND lng BETWEEN 151.187384
            AND 151.295899
    ) AS properties
WHERE `properties`.`deleted_at` IS NULL

UNION

SELECT *
FROM (
    SELECT properties.*
        ,(111.045000 * DEGREES(ACOS(COS(RADIANS(- 33.914437)) * COS(RADIANS(lat)) * COS(RADIANS(151.241642 - lng)) + SIN(RADIANS(- 33.914437)) * SIN(RADIANS(lat))))) AS distance
    FROM `properties`
    WHERE `properties`.`deleted_at` IS NULL
        AND lat BETWEEN - 33.959464
            AND - 33.869411
        AND lng BETWEEN 151.187384
            AND 151.295899
    ) AS properties
WHERE `properties`.`deleted_at` IS NULL Limit 10
    ,10

This may help you as you need but you need to little bit work out for preparing query

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77