我现在有个基础型号表为A表, 我又创建了个B表为型号组表,B表的数据来源A表, 现在我创建了2个型号组为“型号A”,“型号B”,
A表
型号 属性
W-1 门套
W-2 门套
W-3 门套
W-4 门套
T-5 木门
T-6 木门
T-7 木门
B表
组名 型号 属性
型号A W-1 门套
型号A W-2 门套
型号A W-3 门套
型号B T-5 木门
型号B T-6 木门
在具体匹配型号的时候是排除选种的型号。也就是说根据排除B表数据into到C表, 才能使用
C表
组名 型号 属性
型号A W-4 门套
型号B T-6 木门帮我分析下写出来。
A表
型号 属性
W-1 门套
W-2 门套
W-3 门套
W-4 门套
T-5 木门
T-6 木门
T-7 木门
B表
组名 型号 属性
型号A W-1 门套
型号A W-2 门套
型号A W-3 门套
型号B T-5 木门
型号B T-6 木门
在具体匹配型号的时候是排除选种的型号。也就是说根据排除B表数据into到C表, 才能使用
C表
组名 型号 属性
型号A W-4 门套
型号B T-6 木门帮我分析下写出来。
select * from B where not exists(select 1 from A where B.型号=A.型号)
select a.组名,b.型号,b.属性
from B as a join A as b on a.属性 = b.属性
where not exists (select 1 from b where 型号 = b.型号)
insert into c
select a.组名,b.型号,b.属性
from B as a join A as b on a.属性 = b.属性
where not exists (select 1 from b where 型号 = b.型号)
select a.组名,b.型号,b.属性
from B as a join A as b on a.属性 = b.属性
where not exists (select 1 from b where 型号 = b.型号)
declare @A表 table (型号 varchar(3),属性 varchar(4))
insert into @A表
select 'W-1','门套' union all
select 'W-2','门套' union all
select 'W-3','门套' union all
select 'W-4','门套' union all
select 'T-5','木门' union all
select 'T-6','木门' union all
select 'T-7','木门'declare @B表 table (组名 varchar(5),型号 varchar(3),属性 varchar(4))
insert into @B表
select '型号A','W-1','门套' union all
select '型号A','W-2','门套' union all
select '型号A','W-3','门套' union all
select '型号B','T-5','木门' union all
select '型号B','T-6','木门'insert into 表C
select distinct c.组名,a.型号,a.属性 from @A表 a left join
@B表 b on a.型号=b.型号 and a.属性=b.属性
left join @B表 c on left(a.型号,2)=left(c.型号,2) where b.型号 is null
/*
组名 型号 属性
----- ---- ----
型号A W-4 门套
型号B T-7 木门
*/
A表的属性是“复合实木门扇” “锁”什么的, 跟B表的属性对应不上。 B表的属性是手动输入进去的,这俩边唯一有关联的就是 型号, 如果用指针写循环, 就出来了。只要不在选种里面就插入C表, 你们不要用属性关联, 关联不到的
insert into @T1
select 'W-1', '门套' union all
select 'W-2', '门套' union all
select 'W-3', '门套' union all
select 'W-4', '门套' union all
select 'T-5', '木门' union all
select 'T-6', '木门' union all
select 'T-7', '木门'declare @T2 table (组名 varchar(5), 型号 varchar(3), 属性 varchar(4))
insert into @T2
select '型号A', 'W-1', '门套' union all
select '型号A', 'W-2', '门套' union all
select '型号A', 'W-3', '门套' union all
select '型号B', 'T-5', '木门' union all
select '型号B', 'T-6', '木门'select case when a.属性 = '门套' then '型号A' when a.属性 = '木门' then '型号B' end 组名, a.型号, a.属性
from @T1 a left join @T2 b on a.型号 = b.型号
where b.型号 is null
insert into @T1
select 'W-1', '门套' union all
select 'W-2', '门套' union all
select 'W-3', '门套' union all
select 'W-4', '门套' union all
select 'T-5', '木门' union all
select 'T-6', '木门' union all
select 'T-7', '木门'declare @T2 table (组名 varchar(5), 型号 varchar(3), 属性 varchar(4))
insert into @T2
select '型号A', 'W-1', '门套' union all
select '型号A', 'W-2', '门套' union all
select '型号A', 'W-3', '门套' union all
select '型号B', 'T-5', '木门' union all
select '型号B', 'T-6', '木门'select '型号' + left(a.型号, 1) 组名, a.型号, a.属性
from @T1 a left join @T2 b on a.型号 = b.型号
where b.型号 is null