views:

33

answers:

2

I want to do the following query:

SELECT getDistance(lat1, lng1, lat2, lng2) as `distance`, `forename`, `surname`, `etc..`
WHERE `distance` < 10
ORDER BY `distance` ASC

Obviously it won't work because you cannot do WHERE clauses on column 'alias'... can anyone help with an alternative??

getDistance() is a custom procedure... this returns the distance in miles between 2 sets of coordinates. (it is a implementation of http://en.wikipedia.org/wiki/Great-circle_distance)...

Many thanks.

+1  A: 
SELECT  *
FROM    (
        SELECT getDistance(lat1, lng1, lat2, lng2) as `distance`, `forename`, `surname`, `etc..`
        ) q
WHERE   `distance` < 10
ORDER BY
        `distance` ASC
Quassnoi
i wonder if this syntax will work with Oracle and sql server 2005... i can think of some places where it would be helpful
CheeseConQueso
A: 

you could do this, althought it is probably going to slow something up depending on how much action happens in the getDistance sub...

SELECT getDistance(lat1, lng1, lat2, lng2) as `distance`, `forename`, `surname`, `etc..`
WHERE getDistance(lat1, lng1, lat2, lng2) < 10
ORDER BY `distance` ASC
CheeseConQueso