In this query:
SELECT COUNT(*) AS UserCount, Company.* FROM Company
LEFT JOIN User
ON User.CompanyId = Company.Id
WHERE Company.CanAccessSystem= true
AND(User.CanAccessSystem IS null OR User.CanAccessSystem = true)
GROUP BY Company.Id
I want to query a list of companies that can access a particular system as well as the number of users who can access the system inside the company.
This query works for all cases except for one very important one. If a company can access a the system but none of the users can, the Company disappears completely from the query (i.e.: Users.CanAccessSystem = false). In that case, I just want the UserCount = 0.
Example From Companies that Can Access the System:
Users Company Name
1 WidgetWorks
3 WidgetCompany
0 WidgesRUs
This system is on MySQL
Query Edit: Fixed a Typo "ON User.CompanyId = Company.Id"