sqlserver使用with公用表表达式来简化表联查用not exists来筛选结果替代not in。
对于中间结果集被其他表联查多次使用的情况,可以考虑使用with来先计算中间结果集,避免数据库多次重复计算中间结果,用not exists替代not in以使用索引提高查询效率
--排除栏目
with notChannelIds as (
select * from [BaseDict] where id in ('34f3c834325d4ef8bc3a6fe5129c7ee5','ddf85e93bfd74c3e8bca6c4b6d077e5b','4926747949c349679e1ed1677d98db08','62fab521ab0f4f9f849a501f807c4505','c2b156a3ef664b96a6204af19ff07778')
)
SELECT
a.Title,a.ChannelId
,b.Title
FROM [Article] as a
left join BaseDict as b on b.id=a.ChannelId
-- 排除栏目
where not exists ( select 1 from notChannelIds where id=a.ChannelId )