根据另一个表中允许的组合验证表中两个字段的组合


Table A: Customer, Item
Table B: Customer, Cust.Group
Table C: Item, Item.Group
Table D: Cust.group, Item.Group

我想根据表 D 中允许的组合验证表 A 中的组合。我的问题是这两个字段都与 D 通过/通过表 B 和表 C 相关。

(表 B 和 C 对一个查询有多个返回,因为客户和项目都可以在多个组中,这意味着表 A 中的条目可以验证为多个组合(

如何/可以通过简单的选择和连接来解决这个问题?还是我需要在变量、循环等方面更加复杂?

好吧,这应该是有效的行

select * 
from tA 
join tB 
  on tB.cust = tA.cust 
join tC 
  on tC.item = tA.item 
join tD 
  on tD.CustGroup tB.CustGroup 
 and tD.ItemGroup tC.ItemGroup  

这是无效行

select * 
from tA 
join tB 
  on tB.cust = tA.cust 
join tC 
  on tC.item = tA.item 
left join tD 
  on tD.CustGroup tB.CustGroup 
 and tD.ItemGroup tC.ItemGroup 
where tD.CustGroup is null

最新更新