我在数据库中有两个表。如果Numbercalled与电话号码匹配,我想更新IsCompanyNumber,如果不匹配,则调用Yes,然后是No,如果值为Null那么它应该是未知的。我想对NumberCalledFrom做同样的事情,如果它与companynumber表中的Phonenumber列匹配它会说是,如果不匹配,它会说不,如果值是空的,它会说未知。
Create Table Test(
NumberCalled varchar(15)
,IsCompanyNumberCalled Varchar(5)
,NumberCalledFrom varchar(15)
,IscompanyCalledFrom varchar(5)
);
Create Table CompanyNumber(
ID int identity
,PhoneNumber varchar(15)
);
这是我到目前为止所尝试过的。
UPDATE T
Set IsNumberCalled = ‘Yes’,
IsCompanyNumberCalled From = ‘Yes’
FROM CompanyNumber as C
Inner Join Test as T
on T.NumberCalled = C.PhoneNumber AND
T.NumberCalledFrom = C.PhoneNumber;
答案 0 :(得分:0)
对于期望的结果有点困惑,但是如果你想做一个select语句,你会想要做一个case语句会有用吗?
Create Table test2 as (
Select
NumberCalled,
CASE
WHEN a.numbercalled = b.phonenumber then 'Yes'
WHEN b.phonenumber is null then 'Unknown'
ELSE 'No'
END AS IsCompanyNumberCalled,
IsCompanyCalledFrom
from test a
left join companynumber b on a.numbercalled = b.phonenumber);