首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

问个sql语句如何写 高分

2012-01-09 
问个sql语句怎么写 高分!table1有字段ID,A,Dtable2有字段ID,B,E现在想做个视图显示ID,字段A,B并在视图中新

问个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

热点排行