2014-06-26 28 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;