2014-06-26 51 views
0

這可能是重複的問題,但需要幫助。 我是MySQL新手。隱藏MySQL中的別名列

這是我的問題。

我有一個查詢來計算經度和緯度之間的距離。基於距離順序,我必須返回ID的。要顯示

SELECT dlo.id, 
     (3959 * acos(cos(radians(12.9)) * cos(radians(y(gproperty))) * cos(radians(x(gproperty)) - radians(77.5)) +sin(radians(12.9)) * sin(radians(y(gproperty))))) AS distance 
FROM db1.gfeature dgf, 
    db2.loc dlo, 
    db2.cust dcu 
WHERE gf.o_type = 6 
    AND dcu.id = 240 
    AND dgf.o_id = dlo.p_id HAVING distance < 20 
ORDER BY distance LIMIT 10; 

返回作爲

+------+-----------------------+ 
| id | distance    | 
+------+-----------------------+ 
| 101 | 0.00025714756425665 | 
| 199 | 0.10971525612556807 | 
| 722 | 0.22772618588406165 | 
+------+-----------------------+ 

但我只需要ID列。我昨天是asked same-question。但現在我用三個表來獲取數據。在加入3個表格時感到困惑。

有人可以建議我嗎?

我試過這樣

select id from (
    select 
    dlo.id, 
    (3959 * acos( cos(radians(12.9)) 
        * cos(radians(y(gproperty))) 
        * cos(radians(x(gproperty)) - radians(77.5)) 

        + sin(radians(12.9)) 
        * sin(radians(y(gproperty))) 
       ) 
    ) AS distance 
    from db1.gfeature dgf 
     join db2.cust dcu, db2.loc dlo 
     on dgf.o_type = 6 and dcu.id = 10 and dgf.o_id = dlo.w_id 
) t 
where distance < 10 
order by distance 
limit 10; 

,但 「對......」

+2

究竟是什麼問題?只需從選擇列表中刪除表達式即可。 –

+0

可能重複[如何隱藏mysql中的別名列EDITED](http://stackoverflow.com/questions/24400438/how-to-hide-an-alias-column-in-mysql-edited) –

+0

內聯視圖可能幫助你解決你的問題 – Charlesliam

回答

2

越來越語法錯誤附近也許是這樣的。只需使用子查詢來實現。

SELECT S.ID 
FROM 
    (SELECT dlo.id, 
      (3959 * acos(cos(radians(12.9)) * cos(radians(y(gproperty))) * cos(radians(x(gproperty)) - radians(77.5)) +sin(radians(12.9)) * sin(radians(y(gproperty))))) AS distance 
    FROM db1.gfeature dgf, db2.loc dlo , db2.cust dcu 
    WHERE gf.o_type = 6 AND dcu.id = 240 AND dgf.o_id = dlo.p_id 
    HAVING distance < 20) S 
ORDER BY S.distance 
LIMIT 10;