表1
Mail Special Quote (Y/N)
g@hotmail.com Blank
s@ho.com Blank
sss@j.com Blank
表2
Email Address Dept
g@hotmail.com Config
s@ho.com Finance
sss@j.com Marketing
现在我希望表1更新为Y,表2中匹配的电子邮件地址来自Finance
或Marketing
。
UPDATE TABLE 1
Set Special Quote to be Y
where in TABLE 2 the Dept is Finance or Marketing for the matching email address.
可能只是大脑冻结,需要一些咖啡。
想想加入电子邮件地址然后为Y和N位投入一些案例陈述?
答案 0 :(得分:3)
不需要案例陈述。
update table1 set special_quote = 'Y'
where mail in ( select email_address from table2 where dept in ('Finance','Marketing' ))
答案 1 :(得分:1)
尝试以下方法:
UPDATE A
SET A.SpecialQuote = 'Y'
FROM Table1 A
INNER JOIN Table2 B
ON A.Mail = B.EmailAddress
WHERE B.Dept IN ('Finance','Marketing')
答案 2 :(得分:1)
我更像是一个MySQL用户,所以这可能是错的,但尝试这样的事情:
UPDATE table1 SET specialquote = 'Y'
WHERE Mail IN (SELECT EmailAddress FROM table2 WHERE Dept IN ('Finance','Marketing'))
答案 3 :(得分:1)
您非常接近,您需要加入邮件字段中的两个表格,然后检查部门是否在财务/营销部门
UPDATE t1
SET t1.[Special Quote] = 'Y'
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.mail = t2.mail
WHERE t2.Dept in ('Finance','Marketing')
这将仅更新包含的项目,如果您要更新Y / N,则可以执行以下操作(请参阅SQL Fiddle with Demo)
UPDATE t1
SET t1.sq = CASE WHEN t2.dept IS NOT NULL THEN 'Y' ELSE 'N' END
FROM table1 t1
LEFT JOIN table2 t2
ON t1.mail = t2.mail
AND t2.Dept in ('Finance','Marketing')
答案 4 :(得分:0)
你走在正确的轨道上。这是语法:
update table1
set SpecialQuote = 'Y'
where mail in (select emailaddress from table2 where dept in ('Finance', 'Marketing')
答案 5 :(得分:0)
更新表1 设置[特殊报价] ='Y' 来自Table1 t1 加入Table2 t2 在t1。[电子邮件地址] = t2。[电子邮件地址] 其中t2.dept('Finance','Marketing')
未经测试的样本,所以要明白这一点,但相信它足以说明这一点。祝你好运!
编辑:根据OP的“Y / N更新”评论,更改如下......尽管还有其他几种微妙的不同方式可以完成同样的事情。希望这对你有所帮助。
Update Table1
Set [Special Quote]=(case when t2.dept='Finance'
then 'Y'
when t2.dept='Marketing'
then 'Y'
else 'N'
end)
from Table1 t1
join Table2 t2
on t1.[Email Address]=t2.[Email Address]
答案 6 :(得分:0)
create table #tbl1 (mail varchar(50),quote varchar(1))
create table #tbl2 (mail varchar(50),dept varchar(10))
insert into #tbl1 values ('a@a.com','')
insert into #tbl1 values ('b@b.com','')
insert into #tbl1 values ('c@c.com','')
insert into #tbl1 values ('d@d.com','')
insert into #tbl1 values ('e@e.com','')
insert into #tbl2 values ('a@a.com','config')
insert into #tbl2 values ('b@b.com','finance')
insert into #tbl2 values ('c@c.com','marketing')
insert into #tbl2 values ('d@d.com','other')
insert into #tbl2 values ('e@e.com','skivers')
update #tbl1
set quote = 'Y'
where mail in (SELECT mail FROM #tbl2 WHERE dept IN ('finance','marketing'))
select * from #tbl1
drop table #tbl1
drop table #tbl2
答案 7 :(得分:0)
我喜欢使用CTEs来帮助澄清查询的不同部分,因此这是一种方法。如果你不喜欢它还有其他的。
with getval as (
select mail,dept from test2
where dept in ('Marketing','Finance')
)
update test1
set quote = case when dept is NOT null then 'Y' else 'N' end
from test1 t1
left outer join getval t2 on t1.mail = t2.mail
无论如何,我使用左连接来获取所有记录,然后使用案例来排序部门出现的位置(意味着记录在CTE中找到)。
... OR
update table1
set quote = case when dept is NOT null then 'Y' else 'N' end
from table1 t1
left outer join table2 t2 on t1.email = t2.email and t2.dept in ('Marketing','Finance')
那也应该有用。