表中檢索使用組最晚時間排: -通過不工作
表1: - Person_LatLong
Person_Id
Lat
Lng
Location_DateTime
Person_LatLong數據: -
10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42
10 23.03029215373424 72.55710855670746 2014-09-02 04:25:09
10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21
11 19.045563510415214 72.915665750359 2014-09-02 08:22:07
11 19.046567530190785 72.91524330185979 2014-09-02 08:32:36
11 19.04553070382594 72.91621148133476 2014-09-02 08:39:47
11 18.537267778519347 73.83538450865574 2014-09-03 01:44:19
11 18.33554237666039 73.85274219500492 2014-09-03 07:18:02
11 18.331919816746026 73.8525499279805 2014-09-03 07:18:59
11 18.33181875247372 73.85243149060277 2014-09-03 07:19:02
11 18.777939290860722 73.31834934181029 2014-09-04 22:07:31
11 18.790032969638293 73.30265963437363 2014-09-04 22:09:06
11 18.79108238318203 73.29928216416553 2014-09-04 22:09:22
11 18.800857529132163 73.28531940244517 2014-09-04 22:11:22
11 18.812675453346255 73.27794458217039 2014-09-04 22:13:26
11 18.82985965773455 73.25592224937081 2014-09-04 22:15:31
11 18.84531169311457 73.23344887176076 2014-09-04 22:17:49
11 18.869063931831764 73.2185512231118 2014-09-04 22:19:54
11 18.893204517796047 73.20479873759578 2014-09-04 22:22:03
11 18.910161939581506 73.18348844819505 2014-09-04 22:24:30
表2: - LS_For
Subject_Id -> This is basically Person_Id but I have changed the name.
Watcher_Id
Assistance_Group_Id
LS_For表達TA: -
1 10 1
1 11 1
1 17 1
查詢: -
SELECT *,(6371 * acos(cos(radians(23.030))
* cos(radians(Lat)) * cos(radians(Lng) - radians(72.5570))
+ sin(radians(23.030)) * sin(radians(Lat))))
AS distance FROM Person_LatLong WHERE Person_Id IN
(SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1)
HAVING distance < 5 ORDER BY Location_DateTime DESC;
列出了作爲輸出: - ID | LAT | LNG |日期時間|距離
10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21 0.037008818510632306
10 23.03029215373424 72.55710855670746 2014-09-02 04:25:09 0.03433299317128307
10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42 0.02417068347133403
11 23.030301667271765 72.55715279051745 2014-09-02 05:27:21 0.037008818510632306
11 23.03029215373424 72.55710855670746 2014-09-02 05:25:09 0.03433299317128307
11 23.030211421184454 72.55705489668806 2014-09-02 05:23:42 0.02417068347133403
添加GROUP BY後: -
SELECT *,(6371 * acos(cos(radians(23.030))
* cos(radians(Lat)) * cos(radians(Lng) - radians(72.5570))
+ sin(radians(23.030)) * sin(radians(Lat))))
AS distance FROM Person_LatLong WHERE Person_Id IN
(SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1)
GROUP BY Person_Id HAVING distance < 5 ORDER BY Location_DateTime DESC;
給了我下面的輸出: -
10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42 0.02417068347133403
但我想獲得最新的時間排不是最古老的。
需要輸出: - 使用2個表格,LS_For是我在查找Ids的表格,我必須查找位置距離並根據需要根據距離過濾結果。
10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21 0.037008818510632306
11 23.030301667271765 72.55715279051745 2014-09-02 05:27:21 0.037008818510632306
能否請你撥弄你模式和樣本數據? – AK47 2014-09-18 12:18:32