Converting 'WITH CTE' MSSQL to MySQL

筅森魡賤 提交于 2019-12-25 03:12:43

问题


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;

回答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;


来源:https://stackoverflow.com/questions/29113076/converting-with-cte-mssql-to-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!