-1

更新:

这是基于 M Khalid Junaid 回答的演示和当前结果。查询仍然没有输出我的预期结果。

我有一个非常简单的表格,这里是值。

id      animal_id       latitude    longitude   created_at
--------------------------------------------------------------------------
119     75          1.356203    103.828140  2014-04-30 15:00:04
118     75          1.296613    103.857079  2014-04-30 14:58:58
117     75          1.296613    103.857079  2014-04-30 14:58:20
116     75          1.296613    103.857079  2014-04-30 14:53:17

这是我的查询,如果latitudelongitudeuser_id相同,我想进行 GROUP 。

select p.id,p.animal_id,p.name,p.latitude,p.longitude,p.created_at from Photo p
        where 5 >= (select count(*)
                    from Photo p2
                    where p2.animal_id = p.animal_id and
                          p2.id <= p.id
                   )
        AND DATE(p.created_at) > DATE_SUB(NOW(),INTERVAL 13 DAY)
        AND ( p.latitude BETWEEN 0.908862 AND 1.717581 ) AND ( p.longitude BETWEEN 103.584595 AND 104.098206 )
        GROUP BY p.latitude,p.longitude,p.animal_id
        ORDER BY p.created_at DESC;

当前结果 id = 116

id      animal_id       latitude    longitude   created_at
--------------------------------------------------------------------------
119     75          1.356203    103.828140  2014-04-30 15:00:04
116     75          1.296613    103.857079  2014-04-30 14:53:17

预期结果 id = 118

当我对纬度、经度、uer_id 进行分组时,我想获得最新的结果

id      animal_id       latitude    longitude   created_at
--------------------------------------------------------------------------
119     75          1.356203    103.828140  2014-04-30 15:00:04
118     75          1.296613    103.857079  2014-04-30 14:58:58

我尝试了几种方法,但无法获得预期的结果。

4

3 回答 3

1

使用自加入,group by 的结果是不确定的,不能保证你是该组的最新行

select p.id,p.animal_id,
p.latitude,p.longitude,p.created_at 
from Photo p
JOIN (SELECT MAX(id) id,latitude,longitude,animal_id
 FROM Photo 
GROUP BY latitude,longitude,animal_id
) p1 ON(p.id = p1.id)
where 5 >= (select count(*)
            from Photo p2
            where p2.animal_id = p.animal_id and
            p2.id <= p.id)
AND DATE(p.created_at) > DATE_SUB(NOW(),INTERVAL 13 DAY)       
AND ( p.latitude BETWEEN 0.908862 AND 1.717581 ) 
AND ( p.longitude BETWEEN 103.584595 AND 104.098206 )
GROUP BY p.latitude,p.longitude,p.animal_id
ORDER BY p.created_at DESC;

演示

于 2014-05-10T18:45:40.230 回答
0

最后,我设法找到了自己的答案。感谢大家分享您的时间。

SELECT p.id,p.animal_id,p.latitude,p.longitude,p.created_at 

-- This will make sure that `GROUP BY` will pickup most recent result
FROM (SELECT id,animal_id,latitude,longitude,created_at from Photo ORDER BY id DESC) p

-- Pick 5 rows for each animal_id. Meaning, there can be multiple photo of animal but MAX is 5.
WHERE 5 >= (select count(*)
                    from (SELECT * from Photo p4 GROUP BY latitude,longitude,animal_id) p2
                    where p2.animal_id = p.animal_id and
                          p2.id >= p.id 
                   )

-- We want Photos uploaded 5 days ago
AND DATE(p.created_at) > DATE_SUB(NOW(),INTERVAL 15 DAY)

-- Plus, only photos within given Map Bound
AND ( p.latitude BETWEEN 0.908862 AND 1.717581 ) AND ( p.longitude BETWEEN 103.584595 AND 104.098206 )                   

-- This will help to remove duplicate images in same location of same animal. We will see only most recent photo in each exact lat,lng location
GROUP BY p.latitude,p.longitude,p.animal_id

ORDER BY id DESC; 
于 2014-05-11T11:48:48.480 回答
0

这可能是一个棘手的解决方案。您可以像这样在 group 语句中连接纬度、经度、animal_id :

group by concat(latitude, longitude, animal_id)
于 2014-05-11T11:50:58.507 回答