Schema
I have 3 tables with the following columns:
tblClients [ClientID, ClientName, Client Contact]
tblEvents [EventID, EventName]
tblEventBook [EventBookID, EventID, ClientID]
tblEventBook EventIDmatchestblEvents EventIDtblEventBook ClientIDmatchestblClients ClientID
I have two parameters: @useEventID and @useClientID
I would like to retrieve all rows from tblClient where tblEventBook does not equal useEventID and useClientID.
I tried to use the below, but it does not work:
SELECT *
FROM tblClients
WHERE tblEventBook.EventID <> @useEventID
AND tblEventBook.ClientID <> @useClientID
Sample Data:
tblEventBook
EventBookID EventID ClientID
1 1 1
2 2 2
3 3 1
4 4 2
tblClients
ClientID ClientName ClientContact
1 TestNameA 12345
2 TestNameB 54321
tblEvents
EventID EventName
1 TestEventA
2 TestEventB
3 TestEventC
4 TestEventD
5 TestEventE
Required results
I need to get the below results when I use:
-
@useClientID = 1, @useEventID = 1It should return
ClientID 2 -
@useClientID = 2, @useEventID = 1It should return
ClientID 1 and 2 -
@useClientID = 2, @useEventID = 2It should return
ClientID 1 -
@useClientID = 5, @useEventID = 2It should return
ClientID 1 and 2
This solution will exclude all tblClients with @useClientID and related tblEventBook that match @useEventID:
But from your description I also see a slightly difference possibility, where we only exclude tblClients if a related tblEventBook record exists having @useClientID and @useEventID: