0

我正在使用嵌套查询来获取特定半径内的记录。我想按距离对响应进行排序,并将距离作为响应参数之一传递。这是我使用的查询。

select ofr.offerId,ofr.outlet_id,ofr.offer_title,ofr.offer_icon,ofr.offer_description,ofr.CategoryId,ofr.offer_terms,
    ofr.price_description,ofr.rating,ofr.isdeleted,ofr.minpoint_required,otl.shop_name,otl.shop_address,otl.shop_city,otl.shop_phone,otl.shop_icon,
    otl.shop_latitude,otl.shop_longitude,otl.shop_country,otl.shop_zip,distance
from pp.offers as ofr 
join pp.outlets as otl 
where ofr.outlet_id = otl.shop_id and   
MBRContains(GeomFromText(CONCAT('Polygon((',x1,' ', y1,',', x2,' ', y2,',', x3,' ', y3,',',x4,' ', y4,',', x1,' ', y1,'))')),otl.g)
        and match(offer_title,offer_description) against(searchText) 
order by (
    SELECT  glength(LineStringFromWKB(LineString(GeomFromText(astext(PointFromWKB(POINT(latitude,longitude)))), GeomFromText(astext(PointFromWKB(POINT(otl.shop_latitude,otl.shop_longitude)))))))*100 
    AS distance)
    LIMIT 300

但是当试图执行这个我得到一个错误

未知场距

如何在 sql 查询的响应中返回内部查询中计算的距离?

谢谢

4

2 回答 2

2

通过内部查询将您的订单放在 select 语句本身中,并在 orderby 中引用它。按列名排序必须与选择查询列名匹配。

以及检查您从哪个表引用距离字段并将表的别名放在距离前面。

select ofr.offerId, ofr.outlet_id, ofr.offer_title, ofr.offer_icon, ofr.offer_description, 
       ofr.CategoryId, ofr.offer_terms,ofr.price_description, ofr.rating, ofr.isdeleted, ofr.minpoint_required, otl.shop_name,otl.shop_address, otl.shop_city, otl.shop_phone, otl.shop_icon,
       otl.shop_latitude, otl.shop_longitude, otl.shop_country, otl.shop_zip,
       (glength(LineStringFromWKB(LineString(GeomFromText(astext(PointFromWKB(POINT(latitude,longitude)))),GeomFromText(astext(PointFromWKB(POINT(otl.shop_latitude,otl.shop_longitude)))))))*100 
        AS distance
from pp.offers as ofr join
     pp.outlets as otl
     on ofr.outlet_id = otl.shop_id
where MBRContains(GeomFromText(CONCAT('Polygon((',x1,' ', y1,',', x2,' ', y2,',', x3,' ', y3,',',x4,' ', y4,',', x1,' ', y1,'))')),otl.g)
        and match(offer_title,offer_description) against(searchText) order by distance LIMIT 300
于 2014-07-20T12:55:59.577 回答
0

您不能在子查询中定义变量order by并期望在其他任何地方使用它们。如果我理解正确,请将表达式放在 中select,然后在 中引用它order by

select ofr.offerId, ofr.outlet_id, ofr.offer_title, ofr.offer_icon, ofr.offer_description, 
       ofr.CategoryId, ofr.offer_terms,
       ofr.price_description, ofr.rating, ofr.isdeleted, ofr.minpoint_required, otl.shop_name,
       otl.shop_address, otl.shop_city, otl.shop_phone, otl.shop_icon,
       otl.shop_latitude, otl.shop_longitude, otl.shop_country, otl.shop_zip,
       (glength(LineStringFromWKB(LineString(GeomFromText(astext(PointFromWKB(POINT(latitude,longitude)))),
        GeomFromText(astext(PointFromWKB(POINT(otl.shop_latitude,otl.shop_longitude)))))))*100 
        AS distance
from pp.offers as ofr join
     pp.outlets as otl
     on ofr.outlet_id = otl.shop_id
where MBRContains(GeomFromText(CONCAT('Polygon((',x1,' ', y1,',', x2,' ', y2,',', x3,' ', y3,',',x4,' ', y4,',', x1,' ', y1,'))')),otl.g)
        and match(offer_title,offer_description) against(searchText) 

order by distance
LIMIT 300
于 2014-07-20T12:33:34.820 回答