SQLite:查找函数的最小值
我有这些表
Node (#id, route_id, lat, lng)
Route(#id)
并且有四个输入值:$lat1、$lng1、$lat2、$lng2,它们代表 2 个 GPS 坐标 GPS1 和 GPS2。 我还有一个添加到数据库中的距离函数: distance( lat1, lng1, lat2, lng2)
我正在寻找的是为我提供每条路线距离 GPS1 最近点和距离 GPS2 最近点的请求。 所以我想它应该这样开始:
SELECT r.id, n1.lat, n1.lng, n2.lat, n2.lng
FROM Node n1, Node n2
JOIN Route r ON n1.route_id = r.id AND n2.route_id = r.id
我尝试添加
WHERE distance(n1.lat,n1.lng,$lat1,$lng1) = MIN (distance(n1.lat,n1.lng,$lat1,$lng1))
AND distance(n2.lat,n2.lng,$lat2,$lng2) = MIN (distance(n2.lat,n2.lng,$lat2,$lng2))
但它抛出了这个错误:
"misuse of aggregate function MIN()"
我还认为我可以使用
SELECT r.id, n1.lat, n1.lng, n2.lat, n2.lng, distance(n1.lat,n1.lng,$lat1,$lng1) AS d1, distance(n2.lat,n2.lng,$lat2,$lng2) AS d2
FROM Node n1, Node n2
JOIN Route r ON n1.route_id = r.id AND n2.route_id = r.id
GROUP BY r.id
And 对 n1.lat、n1.lng、d1 列按 d1 排序,对 n2 列按 d2 排序。 lat、n2.lng、d2 列,但我不知道该怎么做。
有什么想法吗?
I have those tables
Node (#id, route_id, lat, lng)
Route(#id)
And I have four input values : $lat1, $lng1, $lat2, $lng2, which represent 2 GPS coordinates GPS1 and GPS2.
I also have a distance function that I added to the database : distance( lat1, lng1, lat2, lng2)
What I am looking for is the request that will give me the closest point to GPS1, and the closest point to GPS2 for each route.
So I guess it should start like :
SELECT r.id, n1.lat, n1.lng, n2.lat, n2.lng
FROM Node n1, Node n2
JOIN Route r ON n1.route_id = r.id AND n2.route_id = r.id
And I tried to add
WHERE distance(n1.lat,n1.lng,$lat1,$lng1) = MIN (distance(n1.lat,n1.lng,$lat1,$lng1))
AND distance(n2.lat,n2.lng,$lat2,$lng2) = MIN (distance(n2.lat,n2.lng,$lat2,$lng2))
But it throws this error :
"misuse of aggregate function MIN()"
I also thought I could use
SELECT r.id, n1.lat, n1.lng, n2.lat, n2.lng, distance(n1.lat,n1.lng,$lat1,$lng1) AS d1, distance(n2.lat,n2.lng,$lat2,$lng2) AS d2
FROM Node n1, Node n2
JOIN Route r ON n1.route_id = r.id AND n2.route_id = r.id
GROUP BY r.id
And to sort it by d1 for the n1.lat, n1.lng, d1 columns and by d2 for the n2.lat, n2.lng, d2 columns but I don't know how to do that.
Any idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要执行一个相关查询:(
这个推测我无法测试这一点,如果不准确请原谅我,但应该很接近。我稍后会检查这一点。)
You need to perform a correlated query:
(At this conjecture I am unable to test this, please forgive me if this is not exact, but it should be close. I will check up on this later.)