简体   繁体   中英

Converting 'WITH CTE' MSSQL to MySQL

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM