如何 select 数据取决于来自相同 table 的数据和 SELECT 中的自定义字段?

How to select data depending on data from the same table with custom field in SELECT?

我想查询离机场最近的机场:

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.96632437.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 语法似乎没问题。

mysql 中的错误 1052 表示引用不明确(参见 https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html) 使用正确的别名限定表达式中的所有列,例如:

acos(cos(radians(main.latitude)) *
   cos(radians(neighbour.latitude)) *
   cos(radians(neighbourlongitude) -
   radians(main.longitude)) +
   sin(radians(main.latitude)) *
   sin(radians(neighbour.latitude)))