SQLite SQL 查询的问题
我尝试在 SQLite 3 中运行以下查询:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
HAVING "distance" <= ?
ORDER BY "distance" ASC;
但出现以下错误:
SQLSTATE[HY000]:一般错误:1 a 之前需要 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;
相同的输出,所有记录均已返回。我已经仔细检查过 - 距离计算正确...我不知道这个查询出了什么问题,有人可以帮助我吗?
I'm trying to run the following query in SQLite 3:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
HAVING "distance" <= ?
ORDER BY "distance" ASC;
But I get the following error:
SQLSTATE[HY000]: General error: 1 a
GROUP BY clause is required before
HAVING
I don't understand why SQLite wants me to group results, but still I tried the following:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
GROUP BY "id"
HAVING "distance" <= ?
ORDER BY "distance" ASC;
And I also tried this:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
GROUP BY "distance"
HAVING "distance" <= ?
ORDER BY "distance" ASC;
No errors, but all records were returned (even those having "distance" > ?
). I also tried doing:
SELECT *,
DISTANCE(latitude, longitude, ?, ?) AS "distance"
FROM "country"
WHERE "id" NOT LIKE ?
AND "distance" <= ?
ORDER BY "distance" ASC;
Same output, all records were returned. I've double checked - the distance is being correctly calculated... I've no idea what's wrong with this query, can someone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果未指定
GROUP BY
子句,则无法指定HAVING
子句。使用:如果不想多次调用 DISTANCE,可以使用子查询:
You can't specify a
HAVING
clause without having specified aGROUP BY
clause. Use:If you don't want to call DISTANCE more than once, you can use a subquery:
更好(更快)的方法可能是在应用 ORDER BY 之前减少 SELECTed 集。我使用这种方法:
...其中 (51.123, 0.123) 是您正在搜索的相对于中心纬度/经度点,并且 0.12 和 0.34 的值用于将搜索范围缩小到纬度/经度平方-在适当大小的球体上(即地球球体上该点的 n 公里× n 公里的正方形,其中大小取决于您所在位置的平均地理分布)。我使用 http://en.wikipedia.org/wiki/Longitude 中的度数长度公式计算出在给定搜索点在地球球体上的位置时这些值应该是多少。
A better (and quicker) approach might be to reduce down the SELECTed set before applying the ORDER BY. I use this kind of approach:
...where (51.123, 0.123) is the centre latitude / longitude point you're searching relative to, and the values of 0.12 and 0.34 are used to narrow down your search to a lat/long square-on-a-sphere of an appropriate size (i.e. a square of n kilometres by n kilometres at that point on the Earth's sphere, where the size depends on the average geographical distribution of your locations). I use the degree length formulae from http://en.wikipedia.org/wiki/Longitude to work out what these values should be given the search point's position on the Earth's sphere.
这是语法错误,当您使用having时,您必须使用“group by”,因为
您的group by查询正在获取具有(“distance”>)的记录,因为数据库规则首先需要数据如果有匹配的记录,那么在按原因过滤记录后,它将对其执行分组。所以你永远不会得到具有(“距离”<)的数据,
如果我错了请纠正
it is syntax error, you must have to use 'group by' when you are using having cause,
your query with group by is fetching records having ("distance" >) because, there is database rule that first of all it takes data with matching records then it will perform group by on it after it it is filtering records by having cause. so you never get data having ("distance" <)
please correct if i am wrong
除了上面正确标记的答案之外,如果您不想调用 DISTANCE 函数两次,请参考 WHERE 子句中的别名,即:
Further to the correct flagged answer above, if you don't want to call DISTANCE function twice, refer to the alias in the WHERE clause, i.e: