My business partner and I are having issues selecting from a MySQL view that has a HAVING clause.
The query simply selects a few fields from the view, determines a distance dynamically with a few calculations, and aliases it as 'distance' - then limits the results to those rows with a distance less than a supplied variable.
The distance is calculated using the Haversine formula, referenced by Google Maps: https://developers.google.com/maps/articles/phpsqlsearch
Here is what I know:
1) When the HAVING clause is removed from the query, it returns all the results in the view successfully, including the calculated 'distance' for each row
2) When the HAVING clause is added to the query, it returns an empty result set
3) We also tried swapping the variable in the HAVING clause to a static number - this also returned an empty result set
The contents of the view seem irrelevant since everything works without the HAVING clause.
Here is the query:
SELECT
restaurantName,
restaurantID,
locationID,
locationCity,
locationState,
locationAddress,
locationLatitude,
locationLongitude,
( 3959 * acos( cos( radians('%s') ) * cos( radians( locationLatitude ) ) * cos( radians( locationLongitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( locationLatitude ) ) ) ) AS distance
FROM newView
HAVING distance < '%s'
ORDER BY distance
Remember that the view calculates the 'distance' for every selected row perfectly without the HAVING clause, so we are convinced the problem lies there... when we take it out, everything works but every row in the view is returned.
Any ideas why the HAVING clause is returning an empty set? Is the HAVING clause not compatible with views?