I think it's not really clear for you which result you want, because you made two statements that are not equal.
When you say :
Im trying to find every Company in our database without any contacts.
the request is
SELECT
Organisations.System_ID
,Organisations.Name
FROM
Organisations
LEFT JOIN Persons ON Persons.perOrgRelationId = Organisations.System_ID
HAVING
COUNT(Persons.System_ID) = 0
GROUP BY
Organisations.System_ID
But when you say
How on earth do I Count the number of Organisations that has no persons related via the perOrgRelationId.
the request is
SELECT
COUNT(Organisations.System_ID) AS NumberOfOrganisations
FROM
Organisations
LEFT JOIN Persons ON Persons.perOrgRelationId = Organisations.System_ID
HAVING
COUNT(Persons.System_ID) = 0
GROUP BY
Organisations.System_ID
These points are important:
- you have to specify a
GROUP BY
clause when you use aggregation functions like
COUNT
or
SUM
.
- if you want the organisations which haven't got anybody related, you have to use a
LEFT JOIN
. An
INNER JOIN
will only return the records which have occurrences in both tables.
- if you want the organisations which haven't got anyone related, why testing for a count greater than zero?
Hope this helps. :)