通常,当我看到使用类似以下内容的SQL时:
select * from employees where epmloyeeTypeId in (select id from type where name = 'emp')
我将其替换为where
:
select e.* from employees e
inner join type t on t.id=e.epmloyeeTypeId and t.name = 'emp'
如果逆是子句not in
(如下面)而不是in
子句,是否可以对逆做同样的事情?
INSERT into Subscriptions(ProjectId, RecordTypeCID, NTID, Active, Added, LastUpdate, UpdateBy)
SELECT @ProjectId, RecordTypeCID, @NTID, 1, GETDATE(), GETDATE(), @NTID
FROM @Check CHK
WHERE CHK.ActiveStatus=1
And Not Exists (SELECT SubscriptionId FROM Subscriptions
WHERE ProjectId=@ProjectId
and NTID=@NTID
and RecordTypeCID = CHK.RecordTypeCID
)
其他注意事项
我可以这样做吗?
INSERT INTO Subscriptions(ProjectId, RecordTypeCID, NTID,Active, Added, LastUpdate, UpdateBy)
SELECT @ProjectId, RecordTypeCID, @NTID,1, GETDATE(), GETDATE(), @NTID
FROM @Check CHK
LEFT JOIN Subscriptions subs ON subs.RecordTypeCID = CHK.RecordTypeCID
AND NTID = @NTID
AND ProjectId = @ProjectId
AND CHK.ActiveStatus = 1
AND subs.SubscriptionId IS NULL