先使用左连接处理,再对null值进行转换即可。 使用select a.a,a1,b1 from a left join b on a.a=b.a 会出现 a a1 b1 1 a x 2 b y 3 c null然后对这个结果处理,也就是把b1的null值变成x即是select a.a,a1,isnull(b1,x) from a left join b on a.a=b.a这样结果就是a a1 b11 a x 2 b y 3 c x
tj_dns(愉快的登山者) 太酷了,佩服!
你俩同时回答的问题啊!coooooooooool
哦 马上第二个问题出来了: 就是B表中有从B1到B100的字段,我都要选到视图中,是不是要用100次isnull(b.b1, (select top 1 b1 from b), isnull(b.b2, (select top 1 b2 from b), 。 。 。 isnull(b.b100, (select top 1 b100 from b)这样的话效率也忒也低了吧
select a.a, a.a1, b.b1,b.b2,...b.b100 from a,b where a.a = b.a union all select a.a, a.a1,c.b1,c.b2,...c.b100 from a,( select top 1 * from b ) as c where not exists (select 1 from b where b.a=a.a)
使用select a.a,a1,b1 from a left join b on a.a=b.a
会出现
a a1 b1
1 a x
2 b y
3 c null然后对这个结果处理,也就是把b1的null值变成x即是select a.a,a1,isnull(b1,x) from a left join b on a.a=b.a这样结果就是a a1 b11 a x
2 b y
3 c x
马上第二个问题出来了:
就是B表中有从B1到B100的字段,我都要选到视图中,是不是要用100次isnull(b.b1, (select top 1 b1 from b),
isnull(b.b2, (select top 1 b2 from b),
。
。
。
isnull(b.b100, (select top 1 b100 from b)这样的话效率也忒也低了吧
union all
select a.a, a.a1,c.b1,c.b2,...c.b100 from a,(
select top 1 * from b ) as c
where not exists (select 1 from b where b.a=a.a)