[英]php mysql select only last 3 rows if they are similar and updated 15 minutes earlier
我的表如下:
場景在2016-03-24 11:41 hrs:
我每隔5分鍾從GPS跟蹤器接收短信並插入數據庫。 挑戰是根據查詢結果發出命令以喚醒並進入睡眠模式GPS跟蹤器。
id | snd_sms_time | lat | lng
41750|2016-03-24 11:40:0|26.215363|84.888593
41749|2016-03-24 11:35:0|26.215363|84.888593
41748|2016-03-24 11:30:0|26.215363|84.888593
41747|2016-03-24 11:25:0|26.215363|84.888593
mysql查詢應返回具有相同lat的最后3行。 而且很長。 所有的拉特。 而且很長。 相似它意味着火車車廂在最后15分鍾內靜止不動,現在我可以將GPS追蹤器置於睡眠模式,時間間隔為15分鍾,以節省電池電量。
場景在2016-03-24 12:00小時:
我現在在2016-03-24 11:56之后的15分鍾后喚醒GPS跟蹤器。(我已經在晚上11點41分將GPS跟蹤器置於睡眠模式)。 而且很長。 3行的ID是不同的(id:41751,41750,41749)
id | snd_sms_time | lat | lng
41751|2016-03-24 11:59:0|26.215370|84.888593
41750|2016-03-24 11:40:0|26.215363|84.888593
41749|2016-03-24 11:35:0|26.215363|84.888593
41748|2016-03-24 11:30:0|26.215363|84.888593
現在查詢應返回0行,因為位置已更改。 有任何想法嗎。 提前致謝。
這是使用exists
和subquery
的解決方案。
select * from message
where exists(
select 1
from (select * from message order by snd_sms_time DESC limit 3) t
group by lat,lng
having count(*) = 3
)
order by snd_sms_time DESC limit 3;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.