这可能是重复的问题,但需要帮助。 我是 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;
但是在“on...”附近出现语法错误
最佳答案
也许是这样的。只需使用子查询即可实现。
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;
关于mysql - 在 MySQL 中隐藏别名列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24423711/