我试图获得保存在两个单独的mysql表中的每个项目的平均延迟。让我更清楚地说明我有两个mysql表,如下所示,
table:monitor_servers
+-----------+-----------------+
| server_id | label |
+-----------+-----------------+
| 1 | a.com |
| 2 | b.com |
+-----------+-----------------+
table:monitor_servers_uptime
+-------------------+-----------+-----------+
| servers_uptime_id | server_id | latency |
+-------------------+-----------+-----------+
| 1 | 1 | 0.4132809 |
| 3 | 1 | 0.4157769 |
| 6 | 1 | 0.4194210 |
| 9 | 1 | 0.4140880 |
| 12 | 2 | 0.4779439 |
| 15 | 2 | 0.4751789 |
| 18 | 2 | 0.4762829 |
| 22 | 2 | 0.4706681 |
+-------------------+-----------+-----------+
基本上,每个域都与两个表中的相同id_number相关联。当我在运行下面的查询时,获取每个项目的平均值。
select monitor_servers.label, avg(monitor_servers_uptime.latency)
from monitor_servers,monitor_servers_uptime
where monitor_servers.server_id = monitor_servers_uptime.server_id
group by monitor_servers.server_id;
查询结束了,
+---------------------+-------------------------------------+
| label | avg(monitor_servers_uptime.latency) |
+---------------------+-------------------------------------+
| a.com | 0.41393792995 |
| b.com | 0.47551423171 |
+---------------------+-------------------------------------+
我的问题是我正在以正确的方式进行,同时获得每个项目的平均值以及如何将每个项目的新平均结果插入到表monitor_servers上的新列中?如果某些延迟行为NULL,会发生什么?
**编辑:我想在一个查询结果中实现的是**
+-----------+----------+------------------+
| server_id | label | avg. |
+-----------+----------+------------------+
| 1 | a.com | 0.41393792995 |
| 2 | b.com | 0.47551423171 |
+-----------+-----------------------------+
提前致谢,
答案 0 :(得分:0)
您的计算似乎是正确的。
您可以使用sql:
ALTER TABLE monitor_servers ADD avg_latency DEFAULT 0.0 NOT NULL
进行AVG计算检查this回答。