这是主要订单表的外观: -
| Order_num | Collection_Num |
+--------------+----------------+
| 20143045585 | 123456 |
| 20143045585 | 789012 |
| 20143045585 | 456897 |
| 20143758257 | 546465 |
+--------------+----------------+
这些是收藏: -
| tops | bottom |
+--------------+----------------+
| 353735 | 745758 |
| 123456 | 789012 |
| 456456 | 456897 |
| 323456 | 546465 |
+--------------+----------------+
期望的输出: -
| Order_num |
+--------------+
| 20143045585 |
此处订单编号20143045585具有来自表编号2中的同一行的顶部和底部(第二表中的每一行形成称为' A Collection'即1个顶部和相应的底部)的特定组合。 我想知道什么 - 所有订单号都有一个顶部和一个相应的底部在' Collection_num'柱。 任何人都可以帮助我使用SQL代码吗?
如果有任何不清楚的地方,请告诉我。
答案 0 :(得分:0)
select Order_num
From table_1 as A
where exists
(select tops from table_2 as B where B.tops = A.Collection_num)
AND
(select bottom from table2 as B where B.bottom = A.Collection_num)
答案 1 :(得分:0)
我假设你只有第一个数据表,每个订单只能有两个相关的集合或更少。也许:
\\ bignumber[3] == '8'
bignumber[3] = 4
\\ bignumber[3] == '4'
答案 2 :(得分:0)
您可以尝试使用子查询
select distinct order_num from #yourorder where collection_num
in (select tops from #yourcollections)
and order_num in
( select order_num from #yourorder where collection_num in
(select bottom from #yourcollections) )
答案 3 :(得分:0)
非常确定这样的事情对你有用。我只是在这里使用ctes来创建测试数据,以便可以查询它。
with Orders (OrderNum, CollectionNum) as
(
select 20143045585, 123456 union all
select 20143045585, 789012 union all
select 20143045585, 456897 union all
select 20143758257, 546465
)
, Collections (CollectionID, tops, bottoms) as
(
select 1, 353735, 745758 union all
select 2, 123456, 789012 union all
select 3, 456456, 456897 union all
select 4, 323456, 546465
)
select o.OrderNum
, t.tops
, b.bottoms
from Orders o
join Collections t on t.tops = o.CollectionNum
join
(
select o.OrderNum
, b.bottoms
, b.CollectionID
from Orders o
join Collections b on b.bottoms = o.CollectionNum
) b on b.CollectionID = t.CollectionID
答案 4 :(得分:0)
以下是我使用的查询:
Select *
From (select A.Order_num, B.Coll_ID, B.Bottoms from Orders_table as A
Join Collections_Table as B
on A.Collection_num = B.Bottoms
) as C
join
(select K.Order_num, M.Coll_ID, M.Tops from Orders_table as K
Join Collections_Table as M
on A.Collection_num = B.Tops
) as D
on C.Orders_B = D.Orders_Num AND C.Coll_ID = D.Coll_ID
)