I have been working on this all day trying several offers from several sites, and I just can't figure it out!
I use MS SQL and I have 3 SQL tables that have the following columns:
USERS
UserID
UserAccounts
UserID
AccountID
Accounts
AccountID
Here is a table chart (ignore data types, only for reference to relationships)
I need to get a list of accounts in which this UserID from the Users table does not have an entry in the UserAccounts table.
The problem I am facing is that many users can be associated with many of the same accounts. So, for example, in the UserAccounts table, this data will be valid:
UserAccounts
------------------
|UserID|AccountID|
------------------
| 1 | 1 |
| 2 | 1 |
| 2 | 3 |
- "IS NULL" "< > @UserID", 2 1, .
? !
:
SELECT Accounts.ID, Accounts.CompanyAgencyName
FROM Accounts LEFT OUTER JOIN
(SELECT UserID, AccountID
FROM MyAccount.UserAccounts
WHERE (UserID = @UserID)) AS DerivedUserAccounts ON Accounts.ID = DerivedUserAccounts.AccountID
WHERE (DerivedUserAccounts.AccountID IS NULL)
ORDER BY Accounts.ID