I’m using the following query which returns the result as below:
SELECT subnets.subnet_id , INET_NTOA( subnets.address ) AS inet_address, subnet_masks.hosts, COUNT(addresses.STATUS) AS status_count
FROM subnets
LEFT JOIN subnet_masks ON subnets.mask_id = subnet_masks.mask_id
LEFT JOIN addresses ON subnets.subnet_id = addresses.subnet_id
WHERE addresses.status = 'allocated'
GROUP BY subnet_id
ORDER BY subnets.address
subnet_id inet_address hosts status_count
91 10.10.10.0 65534 3
71 192.168.1.0 254 6
90 192.168.10.0 254 1
However I want it return the rows that count 0 too, much like this:
subnet_id inet_address hosts status_count
91 10.10.10.0 65534 3
76 172.128.0.0 254 0
71 192.168.1.0 254 6
90 192.168.10.0 254 1
I read that it may be a problem with my JOINs so I’ve tried a number of different ones and I’m getting the same result. Does anyone know what I’m doing wrong?
Your
WHEREclause is filtering out records… move it to yourLEFT JOINto resolve the issue: