[英]How do I write this SQL query with 2 tables?
There is a table called AssociateAuditorMap
with 3 columns AssociateID
, AuditorID
and IsActive
(bool). 有一个名为
AssociateAuditorMap
的表,该表具有3列AssociateID
, AuditorID
和IsActive
(布尔)。
Each AssociateID
will have only one AuditorID
mapped to it.One auditor may have many associates mapped to them. 每个
AssociateID
仅映射有一个AuditorID
,一个审计员可能映射了许多关联。
There is a 2nd table called UserMaster
with 2 columns UserID
and RoleID
. 有一个名为
UserMaster
的第二张表, UserMaster
有2列UserID
和RoleID
。
Now given that Auditors
are in the UserMaster
with RoledID=2
, what is the query to find auditors who do not have any associates mapped to them in the AssociateAuditorMap
table? 现在,假设
Auditors
位于RoledID=2
的UserMaster
,那么在AssociateAuditorMap
表中查找没有映射到任何联系人的审核员的查询是什么?
That is, find Auditors
who do not have any rows in the AssociateAuditorMap
table. 也就是说,找到在
AssociateAuditorMap
表中没有任何行的Auditors
。
How about 怎么样
SELECT
u.UserID, u.RoleID
FROM
dbo.UserMaster u
WHERE
u.RoleId = 2
AND NOT EXISTS (SELECT * FROM dbo.AssociateAuditorMap aam
WHERE aam.AuditorID = u.UserID)
This would list all rows from UserMaster
with a RoleID = 2
(auditors) that have no entry in the AssociateAuditorMap
table with that AuditorID
这将列出所有行
UserMaster
与RoleID = 2
个在没有输入(审计师) AssociateAuditorMap
表与AuditorID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.