这可能是重复的问题,但需要帮助。 我是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;
但在&#34; on ...&#34;
附近遇到语法错误答案 0 :(得分: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;