mysql怎么查询递增,MySQL:选择查询,每5分钟递增

I have a weather database which gets datasets about every 70 seconds (depends on when the weather-station delivers the data).

I want to graph it using Pchart but i have too many samples so the X axis is screwed up.

So i want the data for about every 5 minutes. (or every 30 minutes)

The query i currently have is this:

SELECT time, temp_out FROM wmr200 WHERE date(time) = curdate()

This gets the samples for the last 24 hours but there are too many.

解决方案

The following will get you a sample consisting of any data with a timestamp at :00, :05, :10 ...

SELECT time, temp_out FROM wmr200 WHERE date(time) = curdate()

AND mod(minute(time),5) = 0

I'm using the modulo function to check if the minute part of the time is (0 or) divisible by 5.

If you need only one result for each time segment, we've got to get quite a bit more complex.

SELECT concat(date(time),' ',hour(time),':',round(minute(time)/5,0)*5),

min(temp_out), avg(temp_out), max(temp_out)

FROM wmr200

GROUP BY date(time), hour(time), round(minute(time)/5,0)*5

I don't have access to a MySQL instance to test it out right now, but here's the idea. It groups by every five minutes, by grouping by date, hour, and round(minute(time)/5,0)*5 - which rounds minute to the nearest 5.

It selects the value of time that it should be grouped around, and the min, avg, and max temp_out, as I wasn't sure which of these would best apply to your situation.

If, however, your DB doesn't have data for a five minute stretch (or 30 minute stretch, if that's what you're using), it still may not have data for a sample point.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值