简体   繁体   English

如何将内部查询返回的值传递给外部查询

[英]How to pass a value returned from inner query to outer query

I am using a nested query for fetching records within a certain radius. 我正在使用嵌套查询来获取特定半径内的记录。 I want to order the response by distance and also pass the distance as one of the parameter in response. 我想按距离对响应进行排序,并将距离作为响应中的参数之一传递。 here is the query I a using for same. 这是我用于相同的查询。

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

But when trying to execute this I am getting back an error 但是当尝试执行此操作时,我返回了一个错误

unknown field distance 未知场距离

How can I return the distance calculated in the inner query within the response of the sql query? 如何在sql查询的响应中返回内部查询中计算出的距离?

Thanks 谢谢

Put your order by inner query in select statement itself, and refer it within orderby. 通过内部查询将订单放入select语句本身,并在orderby中引用它。 Order by column name must match select query column name. 按列名排序必须与选择查询列名匹配。

As well as check from which table you are refering distance filed and put alias name of the table in front of distance. 还要检查您从哪个表引用了距离字段,并将表的别名放在距离前面。

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

You cannot define variables in a subquery in the order by and expect to use them anywhere else. 您不能按order by在子查询中定义变量,并且不能期望在其他任何地方使用它们。 If I understand correctly, put the expression in the select and then refer to it in the 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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