Mysql查询从3个表中获取信息

时间:2017-02-17 09:15:55

标签: mysql

我有三张桌子,我需要从中获取信息。 nagios_performance_data nagios_large_performance_data client_circuit_mapping

以下是这些表中的样本数据

nagios_performance_data
 circuit_name               | record_date         | latency | packetloss |
+----------------------------+---------------------+---------+------------+
| Device 1                  | 2016-11-28 04:40:00 |   72.54 |       0.00

nagios_large_performance_data
+----------------------------+---------------------+---------+------------+
| circuit_name               | record_date         | latency | packetloss |
+----------------------------+---------------------+---------+------------+
| Device 1                   | 0000-00-00 00:00:00 |   83.00 |       0.00 |
+----------------------------+---------------------+---------+------------+

client_circuit_mapping
+-------------------------------+---------------------------------------+
| circuit_name                  | groupname                             |
+-------------------------------+---------------------------------------+
| Device1                       |Group1                                 |
+-------------------------------+---------------------------------------+

现在我需要查询这三个表以获得以下输出

client_circuit_mapping.groupname,nagios_performance_data.circuit_name,nagios_performance_data.record_date,nagios_performance_data.packetloss,nagios_performance_data.latency,nagios_large_performance_data.latency

任何人都可以帮我查询以获得此输出

2 个答案:

答案 0 :(得分:0)

只有join这三个表circuit_name

select 
    c.groupname,
    a.circuit_name,
    a.record_date,
    a.packetloss,
    a.latency,
    b.latency
from nagios_performance_data a
join nagios_large_performance_data b on a.circuit_name = b.circuit_name
join client_circuit_mapping on a.circuit_name = c.circuit_name

答案 1 :(得分:0)

您需要根据circuit_name列加入表格:

SELECT A.*, B.group_name, C.latency large_latency FROM nagios_performance_data A
INNER JOIN client_circuit_mapping B ON B.circuit_name = A.circuit_name
INNER JOIN nagios_large_performance_data C ON C.circuit_name = A.circuit_name

请注意,我使用了nagios_large_performance_data.latency列的别名。