我有一个查询要获取离机场最近的机场:
SELECT *,
(
6371 *
acos(cos(radians(55.966324)) *
cos(radians(latitude)) *
cos(radians(longitude) -
radians(37.416573)) +
sin(radians(55.966324)) *
sin(radians(latitude)))
) AS distance
FROM flautru.Airport
HAVING distance < 250
ORDER BY distance;
我正在寻找邻近机场的机场的纬度和经度55.966324
在哪里。37.416573
但为了获得这些坐标,我需要先在单独的查询中获取机场数据,这会减慢速度。我想在只知道机场代码的单个查询中进行此操作,因此我编写了以下 SQL 查询:
SELECT neighbour.*,
(
6371 *
acos(cos(radians(main.latitude)) *
cos(radians(latitude)) *
cos(radians(longitude) -
radians(main.longitude)) +
sin(radians(main.latitude)) *
sin(radians(latitude)))
) AS distance
FROM flautru.Airport neighbour
JOIN flautru.Airport main ON neighbour.code <> main.code
WHERE main.code = 'JFK'
HAVING distance < 250
ORDER BY distance;
但它不起作用。我的数据库只显示错误:
Error Code: 1052. target: flautru.-.primary: vttablet: rpc error: code = InvalidArgument desc = (errno 1052) (sqlstate 23000) (CallerID: unsecure_grpc_client): Sql: "select neighbour.*, :vtg1 * acos(cos(radians(main.latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians(main.longitude)) + sin(radians(main.latitude)) * sin(radians(latitude))) as distance from Airport as neighbour join Airport as main on neighbour.`code` != main.`code` where main.`code` = :vtg2 having distance < :vtg3 order by distance asc", B
我的查询做错了什么?MySQL Workbench 的语法似乎没问题。