我正在尝试在 SQLite 3 中运行以下查询:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
HAVING "distance" <= ?
ORDER BY "distance" ASC;
但我收到以下错误:
SQLSTATE[HY000]: 一般错误: 1 在 HAVING 之前需要一个 GROUP BY 子句
我不明白为什么 SQLite 要我对结果进行分组,但我仍然尝试了以下方法:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
GROUP BY "id"
HAVING "distance" <= ?
ORDER BY "distance" ASC;
我也试过这个:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
GROUP BY "distance"
HAVING "distance" <= ?
ORDER BY "distance" ASC;
没有错误,但返回了所有记录(即使是那些有"distance" > ?
)。我也尝试过:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
AND "distance" <= ?
ORDER BY "distance" ASC;
相同的输出,返回所有记录。我已经仔细检查过 - 距离正在正确计算......我不知道这个查询有什么问题,有人可以帮我吗?