在通配符JOINS上过滤MYSQL结果

时间:2016-09-23 06:44:23

标签: php mysql join

我正在尝试将具有连接的表过滤到3个不同的表,其中一个连接具有外卡作为连接条件的一部分。这导致结果输出额外/不需要的行。我无法弄清楚如何摆脱这些。

该设备包括带有1或2个监控卡的机箱。每个主管卡都有自己的IP地址。 如果机箱仅安装了一个主管卡,则引用为: 01-xxxx:N:xxxx< -N在此位置

如果它有2张监督卡,则引用为:

01-xxxx:P:xxxx&lt ;-P for primary

01-xxxx:S:xxxx< -S for secondary

连接表包含源和目标机箱。 这仅描述了机箱之间的连接。每个机箱仅由01-xxxx:N:xxxx或主交换机控制器01-xxxx:P:xxxx描述。每个源和目的地有两个“路径”EAST(E)和WEST(W)。

我正在尝试获取01-xxxx的IP地址:P:xxxx和 01-xxxx:S:xxxx来自'equipment'表中'connections'表 仅引用主设备01-xxxx:P:xxxx,然后根据“设备”表中的IP地址是否在IP范围内,从第三个表中获取VLAN ID,并正确显示每一行: 东部源和目的地+东部IP和EAST VLAN(基于范围内的IP)
和 WEST源和目的地+ WEST IP和WEST VLAN(基于范围内的IP)(如果存在)
样本设备表:

   `equipment` (`id`, `site_code`, `site_id`, `system_name`, `ip_add`, `mcast`, `sys_loc`, `systype`, `itamname`, `dimetis`, `DNS`) VALUES
(686, '25A2', 'TAS:BAUS:MTWEL', '01-25A2:P:TAS:BAUS:MT_WELINGTON', 172525962, 4013364224, 'ACLE RD,  PRK', 6, '01-25a2-p-tas-baus-mt-welington', 0, 1),
(687, '25A2', 'TAS:BAUS:MTWEL', '01-25A2:S:TAS:BAUS:MT_WELINGTON', 172526090, 4013364224, 'ACLE RD,  PRK', 6, '01-25a2-s-tas-baus-mt-welington', 0, 1);

conenctions表:

`connections` (`id`, `system_name_source`, `port_type`, `slot_source`, `port_source`, `system_name_dest`, `slot_dest`, `port_dest`, `cable`, `side_name`, `side`, `status`) VALUES
(332, '01-54A1:P:TAS:TLS:BATHURST', '1G Tunk', '10', '01', '01-25A2:P:TAS:BAUS:MT_WELINGTON', '12', '01', 'D 25A2 54A1 DD001', '01-54A2:P:TAS:TLS:DAVEY', 'W', 1),
(343, '01-54A2:P:TAS:TLS:DAVEY', '1G Tunk', '10', '01', '01-25A2:P:TAS:BAUS:MT_WELINGTON', '11', '01', 'D 25A2 54A2 DD001', '01-54A1:P:TAS:TLS:BATHURST', 'E', 1);

VLAN / IP范围表:

    `vlan_agg` (`id`, `vl_system_name`, `vlan_id`, `ip_sub`, `bcast`, `cidr`) VALUES
(40, '01-54A2:P:TAS:TLS:DAVEY', 72, 172525952, 172526079, 25),
(41, '01-54A1:P:TAS:TLS:BATHURST', 73, 172526080, 172526207, 25);

我的查询:

  SELECT 

connections.system_name_dest,
connections.side,
tgt.system_name AS system_name,
tgt.ip_add AS dest_ip,
dvlan.vlan_id AS d_vlan_id

FROM connections 
LEFT JOIN equipment tgt on SUBSTRING(tgt.system_name,1,8) like SUBSTRING(connections.system_name_dest,1,8)
LEFT JOIN vlan_agg dvlan on tgt.ip_add BETWEEN dvlan.ip_sub AND dvlan.bcast

where system_name_dest='01-25A2:P:TAS:BAUS:MT_WELINGTON'
Order By side

这会产生四行,其中2行具有不正确的目标IP地址和VLAN:

Array ( [system_name_dest] => 01-25A2:P:TAS:BAUS:MT_WELINGTON [side] => E [system_name] => 01-25A2:S:TAS:BAUS:MT_WELINGTON [dest_ip] => 172526090 [d_vlan_id] => 73 ) 
Array ( [system_name_dest] => 01-25A2:P:TAS:BAUS:MT_WELINGTON [side] => E [system_name] => 01-25A2:P:TAS:BAUS:MT_WELINGTON [dest_ip] => 172525962 [d_vlan_id] => 72 ) 
Array ( [system_name_dest] => 01-25A2:P:TAS:BAUS:MT_WELINGTON [side] => W [system_name] => 01-25A2:P:TAS:BAUS:MT_WELINGTON [dest_ip] => 172525962 [d_vlan_id] => 72 ) 
Array ( [system_name_dest] => 01-25A2:P:TAS:BAUS:MT_WELINGTON [side] => W [system_name] => 01-25A2:S:TAS:BAUS:MT_WELINGTON [dest_ip] => 172526090 [d_vlan_id] => 73 )

正确的输出应该是基于主卡的东部IP地址和东部VLAN以及基于设备表中的辅助卡的West IP和West Vlan:

Array ( [system_name_dest] => 01-25A2:P:TAS:BAUS:MT_WELINGTON [side] => E [system_name] => 01-25A2:S:TAS:BAUS:MT_WELINGTON [dest_ip] => 172526090 [d_vlan_id] => 73 )
Array ( [system_name_dest] => 01-25A2:P:TAS:BAUS:MT_WELINGTON [side] => W [system_name] => 01-25A2:S:TAS:BAUS:MT_WELINGTON [dest_ip] => 172526090 [d_vlan_id] => 73 )

任何帮助将不胜感激。几天来我一直在努力。

1 个答案:

答案 0 :(得分:0)

额外的字段,我添加了足够的信息来开发一个有效的过滤器。 (谢谢@solarflare - 他的问题引发了一个解决方案)

查询现在看起来像:

    SELECT 

connections.system_name_dest,
connections.side,
tgt.system_name AS system_name,
tgt.ip_add AS dest_ip,
dvlan.vlan_id AS d_vlan_id

FROM connections 
LEFT JOIN equipment tgt on SUBSTRING(tgt.system_name,1,8) like SUBSTRING(connections.system_name_dest,1,8)
LEFT JOIN vlan_agg dvlan on tgt.ip_add BETWEEN dvlan.ip_sub AND dvlan.bcast

where system_name_dest='01-25A2:P:TAS:BAUS:MT_WELINGTON' and ((system_name NOT LIKE '%:S:%' and side = 'E') OR (system_name LIKE '%:S:%' and side = 'W') )
Order By side