SQL Query用于替换另一个表的记录

时间:2013-10-20 21:00:29

标签: sql

以下代码会产生错误:

select lastname, firstname, workphone, homephone 
from members if (workphone is null) then workphone = homephone;

我正在尝试从名为lastnames的表中选择firstnamesmembers和您的电话号码。如果成员“workphone is null我需要将其替换为homephone

如果需要,我会非常乐意澄清。

2 个答案:

答案 0 :(得分:0)

select lastname, firstname, 
case when (workphone is null) then homephone  else workphone end as workphone 
, homephone 
from members;

答案 1 :(得分:0)

您可以使用COALESCE函数,该函数返回给定的第一个非null参数:

SELECT lastname, firstname, homephone, COALESCE(workphone, homephone) AS workphone
FROM   members