我提前道歉,我觉得我错过了一些非常愚蠢和简单的东西。(让我们忽略数据库结构,因为我在某种程度上被锁定了)。
我有,让我们使用客户订单-一个订单编号可以被运送到多个地方。为了简单起见,我只说明了三个,但也可以不止这三个(家、办公室、礼物、gift2、礼物3等)
所以我的表格是:
客户订单:
OrderID MailingID
--------------------
1 1
1 2
1 3
2 1
3 1
3 3
4 1
4 2
4 3我需要找到的是已经发送到MailingID 1而不是2的orderID (基本上我需要找到的是上面的orderID 2和3)。
如果重要的话,我使用的是Sql Express 2012。
谢谢
发布于 2014-11-03 11:15:09
也许这可能会有所帮助:
create table #temp(
orderID int,
mailingID int
)
insert into #temp
select 1, 1 union all
select 1, 2 union all
select 1, 3 union all
select 2, 1 union all
select 3, 1 union all
select 3, 3 union all
select 4, 1 union all
select 4, 2 union all
select 4, 3
-- find orderIDs that have been shipeed to mailingID = 1
select
distinct orderID
from #temp
where mailingID = 1
except
-- find orderIDs that have been shipeed to mailingID = 2
select
orderID
from #temp
where mailingID = 2
drop table #temp发布于 2014-11-03 11:51:51
一个带有NOT IN操作符的简单Subquery应该可以工作。
SELECT DISTINCT OrderID
FROM <tablename> a
WHERE orderid NOT IN (SELECT orderid
FROM <tablename> b
WHERE b.mailingID = 2) https://stackoverflow.com/questions/26707084
复制相似问题