是否可以编写SELECT查询来检索时间间隔为60秒的数据?
我想获取数据来制作图表,并且我将数据转储到MySQL数据库中。我有时间戳,目标端口和发送的字节的流:
mysql> select * from eth2_flows limit 1;
+---------+----------+---------+-------+----------+-----------+-------------+---------------+-------------+---------------+---------------+----------------+-----------+----------+-----------+----------+---------------------+----------------------+----------------------+-------------------+-----------------------+------------------------+-----------------+------------------+----------+---------------+---------------+----------+---------------+---------+-----------+
| idx | IN_BYTES | IN_PKTS | FLOWS | PROTOCOL | TCP_FLAGS | L4_SRC_PORT | IPV4_SRC_ADDR | L4_DST_PORT | IPV4_DST_ADDR | LAST_SWITCHED | FIRST_SWITCHED | OUT_BYTES | OUT_PKTS | ICMP_TYPE | SRC_VLAN | IP_PROTOCOL_VERSION | CLIENT_NW_DELAY_USEC | SERVER_NW_DELAY_USEC | APPL_LATENCY_USEC | RETRANSMITTED_IN_PKTS | RETRANSMITTED_OUT_PKTS | OOORDER_IN_PKTS | OOORDER_OUT_PKTS | L7_PROTO | IPV6_SRC_ADDR | IPV6_DST_ADDR | HTTP_URL | HTTP_RET_CODE | HTTP_UA | HTTP_MIME |
+---------+----------+---------+-------+----------+-----------+-------------+---------------+-------------+---------------+---------------+----------------+-----------+----------+-----------+----------+---------------------+----------------------+----------------------+-------------------+-----------------------+------------------------+-----------------+------------------+----------+---------------+---------------+----------+---------------+---------+-----------+
| 1265340 | 40 | 1 | 0 | 6 | 16 | 4805 | 2886729835 | 139 | 2886729734 | 1344280441 | 1344280441 | 40 | 1 | 0 | 0 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | | | 0 | | |
+---------+----------+---------+-------+----------+-----------+-------------+---------------+-------------+---------------+---------------+----------------+-----------+----------+-----------+----------+---------------------+----------------------+----------------------+-------------------+-----------------------+------------------------+-----------------+------------------+----------+---------------+---------------+----------+---------------+---------+-----------+
1 row in set (0.00 sec)
我需要获取数据,并以60秒的间隔对结果进行分组,并添加IN_BYTES和OUT_BYTES ......
是否可以执行此操作,还是必须在PHP中编写一些代码?
答案 0 :(得分:3)
如果您的时间戳存储在LAST_SWITCHED
中,它看起来像是Unix时间戳。这是自1970年以来的秒数。如果这是真的,你可以通过将Unix时间戳除以60来group by
分钟:
select LAST_SWITCHED div 60 as StartOfMinuteInterval
, sum(IN_BYTES) as SumIn
, sum(OUT_BYTES) as SumOut
group by
LAST_SWITCHED div 60
在MySQL中,div
是一个整数除法。因此x div y
等于round(x / y)
。