0

I realize that this doesn't directly translate from MSSQL to MySQL but I'm not not sure how to make it work. Any help that you have is appreciated.

;WITH cte As (
SELECT
    post_id,
    status, 
    dealer, 
    distributor,
    SUM( 
        3959 * acos( 
        cos( radians(%f) ) * 
        cos( radians( lat ) ) * 
        cos( radians( lng ) - radians(%f) ) + 
        sin( radians(%f) ) * 
        sin( radians( lat ) ) 
        ) 
    ) 
    AS DISTANCE
    FROM wp_geodatastore
    GROUP BY post_id, status, dealer, distributor       
)
SELECT post_id, status, dealer, distributor, DISTANCE
FROM cte WHERE (DISTANCE < %d) 
AND status = 'publish' AND dealer = 'on' AND distributor = 'on'
ORDER BY DISTANCE
OFFSET %d ROWS
FETCH NEXT %d ROWS ONLY;
Michelle
  • 113
  • 4
  • 13

1 Answers1

1

Just make it a subquery:

SELECT post_id, status, dealer, distributor, DISTANCE
FROM (SELECT post_id, status, dealer, distributor,
             SUM( 3959 * acos( 
                 cos( radians(%f) ) * 
                 cos( radians( lat ) ) * 
                 cos( radians( lng ) - radians(%f) ) + 
                 sin( radians(%f) ) * 
                 sin( radians( lat ) ) 
                ) 
               ) AS DISTANCE
      FROM wp_geodatastore
      GROUP BY post_id, status, dealer, distributor       
     ) cte
WHERE (DISTANCE < %d) AND
      status = 'publish' AND dealer = 'on' AND distributor = 'on'
ORDER BY DISTANCE
OFFSET %d ROWS
FETCH NEXT %d ROWS ONLY;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786