`P2`.`device_id` AS `remote_port_device_id`,
`P2`.`ifName` AS `remote_ifname`,
`P2`.`ifSpeed` AS `remote_ifspeed`,
`P2`.`ifOperStatus` AS `remote_ifoperstatus`,
`P2`.`ifAdminStatus` AS `remote_ifadminstatus`,
`P2`.`ifInOctets_rate` AS `remote_ifinoctets_rate`,
`P2`.`ifOutOctets_rate` AS `remote_ifoutoctets_rate`,
SUM(IF(`P2_ip`.`ipv4_address` = `M`.`ipv4_address`, 1, 0))
AS `remote_matching_ips`
FROM `ipv4_mac` AS `M`
INNER JOIN `ports` AS `P1` ON `P1`.`port_id`=`M`.`port_id`
INNER JOIN `ports` AS `P2` ON `P2`.`ifPhysAddress`=`M`.`mac_address`
INNER JOIN `devices` AS `D1` ON `P1`.`device_id`=`D1`.`device_id`
INNER JOIN `devices` AS `D2` ON `P2`.`device_id`=`D2`.`device_id`
INNER JOIN `ipv4_addresses` AS `P2_ip` ON `P2_ip`.`port_id` = `P2`.`port_id`
$join_sql
WHERE
as Inner join is expected between (ipv4_mac - ports) , (ipv4_mac - devices ), (ipv4_mac-ipv4_addresses )
but the following join criteria is not between thw tables actually being joined
INNER JOIN `devices` AS `D1` ON `P1`.`device_id`=`D1`.`device_id`
INNER JOIN `devices` AS `D2` ON `P2`.`device_id`=`D2`.`device_id`
INNER JOIN `ipv4_addresses` AS `P2_ip` ON `P2_ip`.`port_id` = `P2`.`port_id`