0
SELECT u.*, zz.contid, zz.lng, zz.lat, zz.zip, zz.city, zz.state, (**calc distance**) AS distance
FROM contacts zz
INNER JOIN users u
ON zz.id = u.id
WHERE cond1 = 1, cond2=2, etc..
GROUP BY u.id
HAVING MIN(distance) < 100

错误:未知的列距离

知道这是为什么吗?以及如何解决?

我很感激任何建议,非常感谢提前!

4

2 回答 2

3

这是什么数据库?如果它类似于 MSSQL,则不能在查询的其他地方使用类似的别名。您必须复制整个别名定义:

SELECT big+ugly+calculation AS foo
...
HAVING (big+ugly_calculation) = bar

或者将查询包装在另一个中:

SELECT *
FROM (  SELECT *, big+ugly+calculation AS foo )
WHERE foo = bar
于 2013-10-29T15:23:15.190 回答
0

您不能在聚合函数中引用别名。将查询移动到子查询中,然后将其聚合到外部查询中。

SELECT *
FROM (
    SELECT u.*, zz.contid, zz.lng, zz.lat, zz.zip, zz.city, zz.state, (**calc distance**) AS distance
    FROM contacts zz
    INNER JOIN users u
    ON zz.id = u.id
    WHERE cond1 = 1, cond2=2, etc..) subquery
GROUP BY id
HAVING MIN(distance) < 100
于 2013-10-29T15:29:06.793 回答