今天需要写一个根据关联查询结果更新数据的sql,mysql中支持这样的语法:
mysql:
UPDATE T1, T2,
[INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2. C1
SET T1.C2 = T2.C2,
T2.C3 = expr
WHERE condition
但是oracle不支持上面的语法,oracle实现上面功能的语法结构如下:
oracle 方式1:
UPDATE table1
SET table1.value = (SELECT table2.CODE
FROM table2
WHERE table1.value = table2.DESC)
WHERE table1.UPDATETYPE='blah'
AND EXISTS (SELECT table2.CODE
FROM table2
WHERE table1.value = table2.DESC);
或者
oracle 方式2:
UPDATE
(SELECT table1.value as OLD, table2.CODE as NEW
FROM table1
INNER JOIN table2
ON table1.value = table2.DESC
WHERE table1.UPDATETYPE='blah'
) t
SET t.OLD = t.NEW
相关文章
- 10-29字符的替换/切片/分割/组合 replace_slice_splite_join 的使用
- 10-29sql join用法学习
- 10-29left join、right join、inner join的区别
- 10-29EF join
- 10-29INSERT 中ON DUPLICATE KEY UPDATE的使用
- 10-29报错:Duplicate keys detected: ‘0‘. This may cause an update error. (key值不唯一问题)
- 10-29关于sql中ON DUPLICATE KEY UPDATE的使用
- 10-29仓库储存,存在添加减少,不存在插入ON DUPLICATE KEY UPDATE
- 10-29Duplicate keys detected: '0'. This may cause an update error
- 10-29sql - INSERT … ON DUPLICATE KEY UPDATE …