我正在尝试从一张表中获取记录,但不包括某些记录(联盟中的订单号)。有人可以告诉我此查询有什么问题吗?运行后没有任何记录。
SELECT * FROM [dbo].[FMD15_18] WHERE [OrderNo] NOT IN ((SELECT OrderNo FROM [dbo].[FMD15_18] WHERE [Item Description] Like '%AP%') UNION ALL SELECT [OrderNo] FROM [dbo].[AP&C] )
我会NOT EXISTS改用:
NOT EXISTS
SELECT t.* FROM [dbo].[FMD15_18] t WHERE NOT EXISTS (SELECT 1 FROM [dbo].[FMD15_18] t1 WHERE t1.OrderNo = t.OrderNo AND t1.[Item Description] Like '%AP%') AND NOT EXISTS (SELECT 1 FROM [dbo].[AP&C] a WHERE a.OrderNo = t.OrderNo);
但是,我怀疑nullable当前查询存在一些问题。如果是这样,那么您需要配合IS NOT NULLin subquery。
nullable
IS NOT NULL
subquery