有2个表,ta,tb 想根据tb定义的字典来更新ta中的project的字段
create table ta
(id int identity(1,1) ,
email varchar(20),
project varchar(20)) create table tb
(id int identity(1,1) ,
email varchar(20),
project varchar(20)) 
SQL希望语法没写错测试数据
insert into ta 
select '[email protected]',null union all
select '[email protected]',null union all
select '[email protected]',null union all
select '[email protected]',null union all
select '[email protected]',null union all
select '[email protected]',nullinsert into tb 
select '[email protected]','A项目' union all
select '@163.com','B项目' union all
select '[email protected]','啊项目' union all
select '[email protected]','哦项目' union all
select '[email protected]','发项目'说明如下:ta表为主表,为了描述简单,我去掉了其他多余的字段。
tb为字典表,作用是根据邮箱设置对应所属的项目
然后ta和tb关联,将ta中email等于tb中email的project属性更新为tb中的project属性
特别地:本来所有163.com的都是属于B项目的,比如:张三@163.com,李四@163.com等
唯独[email protected]被用作A项目的邮箱了
请问这样的SQL要怎么写才方便,(因为[email protected]这个是我在提问的时候假设的,如果交给用户的时候就不知道用户设定的是哪个具体信箱了。所以这点比较头痛)另外,表的结构可以修改,比如增加点什么字段的,只要能完成这个特许的功能。最后,感谢高人,热心人,路人

解决方案 »

  1.   

    关于[email protected]这种特殊情况,邮箱名没什么规律的么?
      

  2.   

    declare @email varchar(100)--传入那个特殊的[email protected]
    set @email='[email protected]'
    --先更新所有相等的情况
    update ta set project=tb.project
    where ta.email =tb.email 
    --再更新所有相似的163
    update ta set project=tb.project
    where ta.email like '%@163.com' and tb.email='@163.com'
    --再单独更新@email
    update ta set project=tb.project
    where ta.email=@email and tb.email=@email
      

  3.   

    不好意思,午休去了,就是每个表都有email啊。字典从表的project是设置好的,然后通过从表的email和主表的email关联。
      

  4.   

    hlf1989 ,不好意思啊。感谢你的热心肠,我先一下。以后再想办法补分给你。