0

表格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 中匹配的电子邮件地址来自Financeor 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 位添加一些 case 语句?

4

8 回答 8

3

不需要案例陈述。

update table1 set special_quote = 'Y'
where mail in ( select email_address from table2 where dept in ('Finance','Marketing' ))
于 2012-07-13T14:17:37.633 回答
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')
于 2012-07-13T14:18:40.280 回答
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')
于 2012-07-13T14:17:56.393 回答
1

我更像是 MySQL 用户,所以这可能是错误的,但请尝试以下操作:

UPDATE table1 SET specialquote = 'Y'
WHERE Mail IN (SELECT EmailAddress FROM table2 WHERE Dept IN ('Finance','Marketing'))
于 2012-07-13T14:18:03.503 回答
0

你在正确的轨道上。这是语法:

update table1
    set SpecialQuote = 'Y'
    where mail in (select emailaddress from table2 where dept in ('Finance', 'Marketing')
于 2012-07-13T14:18:41.470 回答
0

更新 Table1 Set [Special Quote]='Y' from Table1 t1 join Table2 t2 on t1.[Email Address]=t2.[Email Address] where t2.dept in ('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]
于 2012-07-13T14:19:13.390 回答
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
于 2012-07-13T14:20:14.303 回答
0

我喜欢使用CTE来帮助澄清查询的不同部分,所以这里有一种方法可以做到这一点。如果你不喜欢,还有其他的。

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 中找到了记录)。

或者...

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')

这也应该有效。

于 2012-07-13T14:31:23.117 回答