有两个表:
表A
名称 数量
苹果 2表B
名称 单价
苹果 3
苹果1 5
苹果2 6
苹果3 10我现在要这样的效果:
名称 单价 数量
苹果 3 2
苹果1 5 2
苹果2 6 2
苹果3 10 2a join b on a.名称=b.名称 得不到那效果。。现在我该怎样处理?
表A
名称 数量
苹果 2表B
名称 单价
苹果 3
苹果1 5
苹果2 6
苹果3 10我现在要这样的效果:
名称 单价 数量
苹果 3 2
苹果1 5 2
苹果2 6 2
苹果3 10 2a join b on a.名称=b.名称 得不到那效果。。现在我该怎样处理?
declare @表A table([名称] varchar(4),[数量] int)
insert @表A
select '苹果',2select * from @表A
--> 测试数据:@表B
declare @表B table([名称] varchar(5),[单价] int)
insert @表B
select '苹果',3 union all
select '苹果1',5 union all
select '苹果2',6 union all
select '苹果3',10select * from @表B
select * from @表B b left join @表A a on charindex(a.名称,b.名称)>0
declare @表A table([名称] varchar(4),[数量] int)
insert @表A
select '苹果',2select * from @表A
--> 测试数据:@表B
declare @表B table([名称] varchar(5),[单价] int)
insert @表B
select '苹果',3 union all
select '苹果1',5 union all
select '苹果2',6 union all
select '苹果3',10select * from @表B
select b.名称,b.单价,a.数量 from @表B b left join @表A a on charindex(a.名称,b.名称)>0
----------------------------
名称 单价 数量
苹果 3 2
苹果1 5 2
苹果2 6 2
苹果3 10 2