2014-07-20 63 views
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查詢?

感謝

+0

哪裏有表距離字段? – Govind

回答

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 
2

在SELECT語句本身內部查詢把你的訂單,排序依據內引用它。按列名稱排序必須與選擇查詢列名稱匹配。

以及檢查從哪個表引用距離提交併將表的別名在距離的前面。

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 
相關問題