问个sql语句怎么写 高分!
table1有字段ID,A,D table2有字段ID,B,E
现在想做个视图
显示 ID,字段A,B 并在视图中新加字段C,并对C赋值
if B为空 then C=A else C=B
两张表通过table1.ID=table2.ID关联
请问sql怎么写
------解决方案--------------------select table1.ID, A, B, isnull(B,A) as C
from table1, table2
where table1.ID=table2.ID
------解决方案--------------------select a.ID,a.A,b.B,C= case when B= " " then A else B from table1 a,table2 b where a.ID = b.ID
------解决方案-----------------------创建视图
create view V_Dis
as
select a.ID, a.A, b.B, C=isnull(b.B,a.A)
from table1 a inner join table2 b
on a.id=b.id
go
---查看视图
select * from V_Dis
------解决方案----------------------
create view v_1
as
select t.id,t.a,t1.b,c=isnull(t1.b,t.a)
from table1 t inner join table2 t1 on t.ID=t1.ID
GO
select * from v_1