我正在尝试使用SQL中以前的列中的计算。我尝试使用变量,但没有运气。这就是我现在所拥有的:
CREATE VIEW CalculationsTable (id, deltaLat, deltaLon, a, c, d) AS
SELECT Resource.id,
RADIANS("+lat+"-Resource.lat) AS deltaLat,
RADIANS("+lon+"-Resource.lon) AS deltaLon,
(SIN(deltaLat/2)*SIN(deltaLat/2)) + COS("+lat+")
* cos(Resource.lat) * (SIN(deltaLon/2)
* SIN(deltaLon/2)) AS a,
2 * ATAN2(SQRT(a), SQRT(1-a)) AS c,
6371 *c AS d
FROM Resource AS Resource;
我继续收到此错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'deltaLat'
错误中的deltaLat
是代码deltaLat
的这一部分的"(SIN(deltaLat/2)..."
答案 0 :(得分:1)
您可以尝试重新格式化查询并使用子表。 E.g。
SELECT calcValueA + 3,
FROM (SELECT A + B calcValueA FROM yourTable);
对于您的查询,您必须多次执行此操作。
答案 1 :(得分:0)
在计算完成时,deltaLat和deltaLon列没有别名,因此您无法引用它们。您需要将别名列包装在自己的查询中,然后将其用作子查询。我已经在下面(仅在编辑器中),即使它不起作用你应该得到一般的想法!
我很自然地把你引用的条款留在了 - 不知道它们的意图是什么
CREATE VIEW CalculationsTable (id, deltaLat, deltaLon, a, c, d) AS
SELECT id,
deltaLat,
deltaLon,
a,
c,
6371 * c AS d
FROM (
SELECT id,
deltaLat,
deltaLon,
a,
2 * ATAN2(SQRT(a), SQRT(1-a)) AS c
FROM (
SELECT id,
deltaLat,
deltaLon,
(SIN(deltaLat/2)*SIN(deltaLat/2)) + COS("+lat+")
* cos(lat) * (SIN(deltaLon/2)
* SIN(deltaLon/2)) AS a
FROM (
SELECT id,
RADIANS("+lat+"-Resource.lat) AS deltaLat,
RADIANS("+lon+"-Resource.lon) AS deltaLon
Resource.lat as lat
FROM Resource
)
)
)