I am trying to work out if it is possible to write the following using Drupal's database abstraction layer. The tricky bit is the second field which is a function to calculate distance. Is it possible to include this calculation field (3959 * acos(cos(radians(:lat)) * cos(radians(field_bluescope_geolocation_lat)) * cos(radians(field_bluescope_geolocation_lng) - radians(:lng)) + sin(radians(:lat)) * sin(radians(field_bluescope_geolocation_lat)))) AS distance
using the abstraction layer?
$result = db_query_range('SELECT '
. 'geo.entity_id, '
. '(3959 * acos(cos(radians(:lat)) * cos(radians(field_bluescope_geolocation_lat)) * cos(radians(field_bluescope_geolocation_lng) - radians(:lng)) + sin(radians(:lat)) * sin(radians(field_bluescope_geolocation_lat)))) AS distance '
. 'FROM '
. 'node__field_bluescope_geolocation AS geo '
. 'INNER JOIN '
. 'node__field_bluescope_consumer_supp AS consumer_supp '
. 'ON '
. 'geo.entity_id = consumer_supp.entity_id '
. 'WHERE '
. 'geo.bundle = \'bluescope_business\' '
. 'AND '
. 'consumer_supp.field_bluescope_consumer_supp_target_id = 28 '
. 'ORDER BY distance', 0, 200, [':lat' => $lat, ':lng' => $lng]);