我有疑问:
SELECT busCode, address, current.position-1 as position, movingbus.bus as bus,
current.linea as line, destination, current.paletta as 'end',
previous.paletta as 'start', previous.lapseToNext, current.ident,
movingbus.latitude, movingbus.longitude,
SQRT(POW(movingbus.latitude-41.717149, 2) +
POW(movingbus.longitude-12.312499,2)) as distance,
ROUND(distance*80) as `time`,
current.previous as previous
FROM striscie as current, striscie as previous, movingbus,
linee, palette
WHERE palette.number=current.paletta
AND current.linea=line
AND current.previous=previous.ident
AND current.ident=movingbus.striscia
HAVING distance<0.01
其中我计算一个遥远的,然后计算一个距离的时间:相对部分是:
SQRT(POW(movingbus.latitude-41.717149,2)+
POW(movingbus.longitude-12.312499,2)) as distance,
ROUND(distance*80) as `time`
然而,当我执行它时,MariaDB报告错误:
“字段列表”中的未知列“距离”
如果我反过来在“时间”中用'完整操作'代替'距离',那么产生它:
SQRT(POW(movingbus.latitude-41.717149,2)+
POW(movingbus.longitude-12.312499,2)) as distance,
ROUND(SQRT(POW(movingbus.latitude-41.717149,2)+
POW(movingbus.longitude-12.312499,2))*80) as `time`
我没有错误。但是我当然得到一个非常大且可能效率低下的查询。如何在同一个select中的操作中使用select返回的字段作为参数?
答案 0 :(得分:0)
计划A:
@dist := SQRT(...) AS distance,
ROUND(@dist*80) as `time`,
计划B:计算子查询中的距离(以及许多其他值),然后在外部查询中使用distance
两次。
对于此案例,计划A简单而有效。如果有GROUP BY
,则可能根本不起作用。
计划B在这个的情况下效率不高,但是在到达distance<0.1
之前不必携带大量的东西可以带来巨大的性能优势。我在一小时前讨论过这个问题:https://stackoverflow.com/a/35905072/1766831使用这种技术可以提高性能,而不是从计划A中获得的效果。
答案 1 :(得分:0)
正确:查询转到:
SELECT busCode, address, current.position-1 as position, movingbus.bus as bus,
current.linea as line, destination, current.paletta as 'end',
previous.paletta as 'start', previous.lapseToNext, current.ident,
movingbus.latitude, movingbus.longitude,
@dist := SQRT(POW(movingbus.latitude-41.717149, 2) +
POW(movingbus.longitude-12.312499,2)) as distance,
ROUND(@dist*80) as `time`,
current.previous as previous
FROM striscie as current, striscie as previous, movingbus,
linee, palette
WHERE palette.number=current.paletta
AND current.linea=line
AND current.previous=previous.ident
AND current.ident=movingbus.striscia
HAVING distance<1
它确实有效,非常感谢。