簡體   English   中英

如何將內部查詢返回的值傳遞給外部查詢

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

我正在使用嵌套查詢來獲取特定半徑內的記錄。 我想按距離對響應進行排序,並將距離作為響應中的參數之一傳遞。 這是我用於相同的查詢。

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查詢的響應中返回內部查詢中計算出的距離?

謝謝

通過內部查詢將訂單放入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

您不能按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