我使用嵌套查询来获取特定半径内的记录。我想按距离对响应进行排序,并将距离作为响应中的参数之一传递。这是我使用的查询。
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查询的响应中返回内部查询中计算的距离?
由于
答案 0 :(得分: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
答案 1 :(得分: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