Consider this query:
SELECT s.*, COUNT( ssh_logs.id ) AS ssh_count FROM servers s
LEFT JOIN logs ssh_logs ON s.ip_address = ssh_logs.server_ip
I am under the impression the LEFT JOIN shows all rows on the left table, regardless of whether there’s a match for the ON condition.
SELECT s.* FROM servers s
Returns 12 entries, while the first query returns only 1 where the ip addresses match.
So how do I get the first query to display all the rows in servers table alongwith the joined table data?
The aggregate function count() collapses all rows into one.
Do a
group byto see the count per ip-address.This will work best if servers.ip_address is an unique field for servers (primary key or unique index).
If servers has duplicate ip_addresses than this query will group those ip_addresses together and hide data that should be hidden.
However given the fact that these are servers it is logical to assume ip_address is unique.