SQLite:查找函数的最小值

发布于 2024-09-11 12:15:15 字数 1083 浏览 6 评论 0原文

我有这些表

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2024-09-18 12:15:15

你需要执行一个相关查询:(

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) = (
                                             SELECT MIN (distance(lat,lng,$lat1,$lng1))
                                             FROM Node c_n
                                             WHERE c_n.nid = n1.nid)
AND distance(n2.lat,n2.lng,$lat2,$lng2) = (
                                             SELECT MIN (distance(lat,lng,$lat1,$lng1))
                                             FROM Node c_n
                                             WHERE c_n.nid = n2.nid)

这个推测我无法测试这一点,如果不准确请原谅我,但应该很接近。我稍后会检查这一点。)

You need to perform a correlated query:

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) = (
                                             SELECT MIN (distance(lat,lng,$lat1,$lng1))
                                             FROM Node c_n
                                             WHERE c_n.nid = n1.nid)
AND distance(n2.lat,n2.lng,$lat2,$lng2) = (
                                             SELECT MIN (distance(lat,lng,$lat1,$lng1))
                                             FROM Node c_n
                                             WHERE c_n.nid = n2.nid)

(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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文