sqlite选择分组结果的最小函数值
我有一张包含连锁餐厅 GPS 位置的表,想返回在一定半径内最接近点 (A) 的餐厅的地址
SELECT *
, MIN(distance($lat, $lon, lat, lon)) as miles
FROM all_restaurants
WHERE lat between $lat1 and $lat2
AND lon between $lon1 and $lon2
AND miles < $miles
GROUP BY restaurant_id
ORDER BY miles ASC
, company_name ASC
LIMIT 500
返回错误
滥用聚合:MIN()
I have a table with gps locations of restaurants chains and would like to return the addresses of the restaurants closest to point (A) within a certain radius
SELECT *
, MIN(distance($lat, $lon, lat, lon)) as miles
FROM all_restaurants
WHERE lat between $lat1 and $lat2
AND lon between $lon1 and $lon2
AND miles < $miles
GROUP BY restaurant_id
ORDER BY miles ASC
, company_name ASC
LIMIT 500
returns error
misuse of aggregate: MIN()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您的问题是您仅按
restaurant_id
进行分组。当您使用MIN、MAX、SUM、AVG
等聚合函数时,您需要在SELECT
语句中包含不在任何聚合函数中的每一列。在这种情况下,您有两个选择,要么在SELECT
中仅将restaurant_id
如下所示:或者将“*”中包含的所有其他列放在分组中 (因为您不能使用
GROUP BY *
)。I think that your problem is that you only are grouping by
restaurant_id
. When you use an aggregation function asMIN, MAX, SUM, AVG
, etc, you need to include every column in theSELECT
statement that are not in any aggregation function. In this case you have two options, either in theSELECT
you only putrestaurant_id
as the following:Or you put every other column included in the '*' on the grouping (because you can't use
GROUP BY *
).我敢打赌,您正在调用一个函数
distance($lat, $lon, lat, lon)
,不是吗?您是否尝试过在
$lat
和lat
之间使用简单的减法,并使用MIN
函数返回最小值?我猜,
MIN
的用途是与表字段或一些基本操作(例如减法)一起使用。这可能是遇到错误的原因。最重要的是,在使用聚合函数时避免使用
*
,列出查询中所需的列。也许向我们提供示例数据可能会帮助我们找到解决您问题的方法。
I bet that you're calling a function which is
distance($lat, $lon, lat, lon)
, don't you?Have you tried using a simple subtraction between
$lat
andlat
and have it return the minimum value using theMIN
function?MIN
's purpose is for use with a table field or some basic operation such as a subtraction, I guess. This might be the cause of the encountered error.Up and foremost, avoid using
*
while using aggregation functions, list the columns you require in your query.Perhaps providing us with sample data might help us find a way around to work you through.
拉马克有点正确,但这就是我最终所做的,
希望这对其他人有帮助
Lamak was somewhat right but this is what i ended up doing instead
hope this helps somebody else