views:

41

answers:

1
cursor.execute("SELECT user_id FROM myapp_location WHERE\
       GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) < %s"\
       ,(user_utm_easting, user_utm_northing, 500));

This query selects users which are within 500 feet of the current user. How would I order people by the distance? (the glength). Nearby first, farthest last.

How would you change this query? THanks.

+1  A: 

Here's one way

SELECT user_id
     , GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) as glength
  FROM myapp_location
HAVING glength < %s"
 ORDER BY glength desc
Peter Bailey