Hello,

I've got this query which takes a postcode and returns the nearest results based on the variable $area.

Code:

Code:
SELECT postcode1, postcode2, postcode, name, address1, address2, address3, address4, tel, fax, email, 
website, lat, lng, typ, 
(SQRT(POW((b.x - a.x), 2) + POW((b.y - a.y), 2))/1000) * 0.621 AS distance
		FROM postcodes a, postcodes b, markers
		WHERE typ='standard'
		AND a.outcode LIKE '".$postcode."' AND b.outcode = markers.postcode1
		HAVING (distance < '".$area."')
		ORDER BY distance asc LIMIT 50";
I'm using a geo ip lookup to find out the current users latitude and longitude, and i am wanting to rewrite this query to tell me the nearest postcode result in the database, the table is structed like so:

Postcodes
outcode | x | y | latitude | longitude
PL23 | 212500 | 51900 | 50 | -4
PL24 | 207300 | 54100 | 50 | -4

The other table "markers" is irrelivant for what i'm trying to change this too.

Essentially i want to feed it the latitude and longitude and it to return to me the "outcode" that is nearest the result.

If someone could explain to me what the SQL query is actually doing step by step that would be a bit help, as i'm getting lost when it all starts getting a bit too maths involved. (highlighted)

Thanks