简体   繁体   English

如何用2个表编写此SQL查询?

[英]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列AssociateIDAuditorIDIsActive (布尔)。

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列UserIDRoleID

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=2UserMaster ,那么在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 这将列出所有行UserMasterRoleID = 2个在没有输入(审计师) AssociateAuditorMap表与AuditorID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM