创建视图时出错

时间:2013-02-01 20:40:02

标签: sql view

我有表t1(name,phone,adress),想要创建视图vw_1。我正在寻找基本的逻辑: -

create view vw_1 (col1,col2)
as
if(t1.name is null)then 
select adress,phone from t1
else
select name,phone from t1

1 个答案:

答案 0 :(得分:2)

使用coalesce函数(doc用于SQL Server,但它是一个经典的ANSI运算符,并且在我所知道的DBMS中工作)。

create view vw_1 (col1,col2)
    as
    select coalesce(name, adress), phone 
    from t1

如果您的意思是“空或空”,那么

CREATE VIEW vw_1(col1, col2)
  AS
  SELECT CASE WHEN COALESCE(name, '') = '' THEN adress else name END,
  phone
  FROM t1

请参阅sqlFiddle