互联网服务 数据库

能否用merge 实现这样的功能

merge into zy.temp3 t1 using  zy.temp4 t2
on t1.c1 = t2.c1
when matched then
update set t1.c2 = char(t2.c2)
when not matched then
update set t1.c2 = '未知'
参与20

20 同行回答

foryuling foryuling 系统架构师 skyon
谢谢,骑猪撞上墙 的提醒呀,我这个表是可以为null 的。平时都没有怎么注意,使用UPDATE  zy.temp3 t1        SET   c1 = (SELECT t2.c1 as c1 FROM  zy.temp4 t2 WHERE   t1.c1 = t2.c1) 会产生null...显示全部
谢谢,骑猪撞上墙 的提醒呀,我这个表是可以为null 的。

平时都没有怎么注意,使用
UPDATE  zy.temp3 t1
        SET   c1 = (SELECT t2.c1 as c1 FROM  zy.temp4 t2 WHERE   t1.c1 = t2.c1)

会产生null值:) 收起
互联网服务 · 2009-04-14
浏览601
-- update machedUPDATE  zy.temp3 t1        SET   c1 = (SELECT t2.c1 as c1 FROM  zy.temp4 t2 WHERE   t1.c1 = t2.c1)-- update not machedUPDATE  zy.temp3 t1SET   c...显示全部
-- update mached
UPDATE  zy.temp3 t1
        SET   c1 = (SELECT t2.c1 as c1 FROM  zy.temp4 t2 WHERE   t1.c1 = t2.c1)

-- update not mached
UPDATE  zy.temp3 t1
SET   c1 = (SELECT '未知' as c1 FROM  zy.temp4 t2 WHERE  t1.c1 <>  t2.c1)

引用leo兄的这个,leo兄这个逻辑是对的 foryuling兄试验这个不成功是不是 每次update后除了需要update的东西外其他的都是null对吧
UPDATE  zy.temp3 t1
        SET   c1 = (SELECT t2.c1 as c1 FROM  zy.temp4 t2 WHERE   t1.c1 = t2.c1)
WHERE EXISTS(SELECT 1
                                     FROM zy.temp4 t3
                                  WHERE T1.C1 = T3.C1);
这样的话就应该避免不想update的变成null的情况,
这个只是我的猜想,我不了解你的这几张表的具体情况哦:) 收起
2009-04-14
浏览603
foryuling foryuling 系统架构师 skyon
yangyutao 你这样的语法也是:when matched then (update or delete ) when not matched then insert 我想要的结果是:when matched then (update) when not matched then (update)这样能行吗?显示全部
yangyutao 你这样的语法也是:
when matched then (update or delete )
when not matched then insert

我想要的结果是:
when matched then (update)
when not matched then (update)

这样能行吗? 收起
互联网服务 · 2009-04-14
浏览648
MERGE INTO table USING data_source ON (condition) WHEN MATCHED THEN update_clause WHEN NOT MATCHED THEN insert_clause; MERGE INTO employee AS e          USING (SELECT empno, firstnme, midinit, lastname, workdept, phoneno...显示全部
MERGE INTO table
USING data_source
ON (condition)
WHEN MATCHED THEN update_clause
WHEN NOT MATCHED THEN insert_clause;

MERGE INTO employee AS e
          USING (SELECT empno, firstnme, midinit, lastname, workdept, phoneno,
                  hiredate, job, edlevel, sex, birthdate, salary FROM my_emp) AS m
        ON e.empno = m.empno
        WHEN MATCHED THEN
          UPDATE SET (salary) = (m.salary)
        或
          delete
        WHEN NOT MATCHED THEN
          INSERT (empno, firstnme, midinit, lastname, workdept, phoneno,
                  hiredate, job, edlevel, sex, birthdate, salary)
          VALUES (m.empno, m.firstnme, m.midinit, m.lastname,
                  m.workdept, m.phoneno, m.hiredate, m.job, m.edlevel,
                  m.sex, m.birthdate, m.salary)

可以参考这个例子试试 对上面 SQL 的解释如下

使用表 my_emp 联合表 employee,根据两个表的比照(ON e.empno = m.empno),
如果存在这样的记录,则使用 表 my_emp 的 salary 值来更新表 employee 的 salary 的值或删除;
如果不存在符合条件的记录,则将表 my_emp 中的数据插入到表 employee 中去。
可以理解为 先更新数据,更新不了就插入数据或删除。
可以省略其中一个update、delete 或insert子句 收起
2009-04-14
浏览644
foryuling foryuling 系统架构师 skyon
还不其它办法吗? leo刚才测试了你的语句,这样不行。显示全部
还不其它办法吗? leo刚才测试了你的语句,这样不行。 收起
互联网服务 · 2009-04-13
浏览609
leo_wyn leo_wyn 商业智能工程师 Security
刚开始没有明白你的意思 :lol显示全部
刚开始没有明白你的意思 :lol 收起
系统集成 · 2009-04-13
浏览619
leo_wyn leo_wyn 商业智能工程师 Security
这个最佳答案应该是6# 正解:LTry the following statement:-- update machedUPDATE  zy.temp3 t1SET   c1 = (SELECT t2.c1 as c1 FROM  zy.temp4 t2 WHERE   t1.c1 = t2.c1)-- update not machedUPDATE  zy.temp3 t1...显示全部
这个最佳答案应该是6# 正解:L

Try the following statement:

-- update mached
UPDATE  zy.temp3 t1
SET   c1 = (SELECT t2.c1 as c1 FROM  zy.temp4 t2 WHERE   t1.c1 = t2.c1)

-- update not mached
UPDATE  zy.temp3 t1
SET   c1 = (SELECT '未知' as c1 FROM  zy.temp4 t2 WHERE  t1.c1 <>  t2.c1) [ 本帖最后由 leo 于 2009-4-13 14:45 编辑 ] 收起
系统集成 · 2009-04-13
浏览605
leo_wyn leo_wyn 商业智能工程师 Security
NOT MATCHED Indicates the operation to be performed on the rows where the ON search condition is false or unknown. Only INSERT or signal-statement can be specified after THEN.显示全部
NOT MATCHED
Indicates the operation to be performed on the rows where the ON search condition is false or unknown. Only INSERT or signal-statement can be specified after THEN. 收起
系统集成 · 2009-04-13
浏览610
foryuling foryuling 系统架构师 skyon
嗯,是的。想了半天,还是手工改好了!显示全部
嗯,是的。想了半天,还是手工改好了! 收起
互联网服务 · 2009-04-13
浏览623
存储过程也行,哈哈, 在者你就直接update,不过这样得分两步,你想一步的话,估计有点危险显示全部
存储过程也行,哈哈, 在者你就直接update,不过这样得分两步,你想一步的话,估计有点危险 收起
2009-04-13
浏览597

提问者

foryuling
系统架构师 skyon
擅长领域: 数据库Linux服务器
评论739

相关问题

相关资料

相关文章

问题状态

  • 发布时间:2009-04-13
  • 关注会员:0 人
  • 问题浏览:7467
  • 最近回答:2009-04-14
  • X社区推广