X
Xenophobe
I'm stuck trying to solve the following NULL query. I know why the query
isn't working, but am uncertain on how to solve it.
There are two tables, ServiceTypes and ServiceCompanies:
Table: ServiceTypes
Columns: ServiceTypeID, ServiceTypeName
Table: ServiceCompanies
Columns: ServiceTypeID, CompanyID
ServiceTypes and CompanyID record combinations must be unique. In other
words a company is only allowed to have one of each service type associated
with it.
ServiceTypes contains 5 different service types:
ServiceTypeID, ServiceTypeName
1, Plumbing
2, Electrical
3, Flooring
4, Drywall
5, Landscaping
A single company can offer 1 or more services. These are stored in
ServiceCompanies bridge table:
ServiceTypeID, CompanyID
1, 1
2, 1
3, 1
1, 2
2, 2
....and so on.
So here's my challenge (finally!) I would like to return a recordset of
services that are NOT currently associated with a company. This would allow
them to add any additional services that aren't already defined.
I have played with many variations of the following query and read the docs,
but without success.
SELECT ST.ServiceTypeID FROM ServiceTypes ST
LEFT JOIN ServiceCompanies SC ON ST.ServiceTypeID = SC.ServiceTypeID
WHERE SC.ServiceTypeID IS NULL and SC.CompanyID = ?
No matter what, I always get zero results. The reason for this seems to be
that the query finds other references to ServiceTypeID in ServiceCompanies
and is ignoring the CompanyID specific portion of the WHERE clause.
I have been unable to find the right syntax. Any suggestions would be
greatly appreciated!
isn't working, but am uncertain on how to solve it.
There are two tables, ServiceTypes and ServiceCompanies:
Table: ServiceTypes
Columns: ServiceTypeID, ServiceTypeName
Table: ServiceCompanies
Columns: ServiceTypeID, CompanyID
ServiceTypes and CompanyID record combinations must be unique. In other
words a company is only allowed to have one of each service type associated
with it.
ServiceTypes contains 5 different service types:
ServiceTypeID, ServiceTypeName
1, Plumbing
2, Electrical
3, Flooring
4, Drywall
5, Landscaping
A single company can offer 1 or more services. These are stored in
ServiceCompanies bridge table:
ServiceTypeID, CompanyID
1, 1
2, 1
3, 1
1, 2
2, 2
....and so on.
So here's my challenge (finally!) I would like to return a recordset of
services that are NOT currently associated with a company. This would allow
them to add any additional services that aren't already defined.
I have played with many variations of the following query and read the docs,
but without success.
SELECT ST.ServiceTypeID FROM ServiceTypes ST
LEFT JOIN ServiceCompanies SC ON ST.ServiceTypeID = SC.ServiceTypeID
WHERE SC.ServiceTypeID IS NULL and SC.CompanyID = ?
No matter what, I always get zero results. The reason for this seems to be
that the query finds other references to ServiceTypeID in ServiceCompanies
and is ignoring the CompanyID specific portion of the WHERE clause.
I have been unable to find the right syntax. Any suggestions would be
greatly appreciated!