Oracle两(多)表关联更新操作。Oracle基于一张表更新另一张表的多个或者一个字段。
文章目录
- 1、样例一:关联另外一张表将某一个字段更新为固定值
- 1.1、使用方式一(update…set…where exists…)来实现
- 1.2、使用方式二(merge into…when matched…)来实现
- 2、样例二:将一张表的一个字段更新为另外一张表的某个字段的值
- 2.1、使用方式一(update…set…where exists…)来实现
- 2.1.1、注意事项
- 2.2、使用方式二(merge into…when matched…)来实现
- 3、样例三:将一张表的多个字段更新为另外一张表的某些字段的值
- 3.1、使用方式一(update…set…where exists…)来实现
- 3.1.1、注意事项
- 3.2、使用方式二(merge into…when matched…)来实现
在实际项目中经常会碰到一个表的数据update依赖其它表数据的情况。
这里介绍两种方式来实现这种情况下的需求。
方式一:update…set…where exists…
方式二:merge into…when matched…
按照下面的图片创建 2 个表,并插入一些数据。
1、样例一:关联另外一张表将某一个字段更新为固定值
给某个字段更新一个固定的值,但需要关联另外一张表连接到的数据才进行更新
1.1、使用方式一(update…set…where exists…)来实现
update
a_test_two a
set
name = 'vip-name'
where
exists (
select 1 from a_test_one b where a.id = b.id
);
执行后的结果:
1.2、使用方式二(merge into…when matched…)来实现
merge into a_test_two a
using (select b.id, b.name from a_test_one b where b.id in (1,2)) t
on (t.id = a.id)
when matched then
update set a.name = 'vip-name';
/**
* 看不懂的话,可以尝试着用英语的角度去读,然后大致通过英语单词来翻译一下,就知道是怎么回事了。
*
* 从英语的角度可以看出:
* merge into a_table a 合并于某一张表
* using ( 一个结果集 ) r 使用某一个结果集
* on a.xxx = r.xxx 条件
* when matched then 当条件匹配的时候
* update set a.xxx = 'vip-name'; 更新a表的字段
*/
/**
* 如果说表中没设置 ID 主键,没办法保证匹配条件的时候还是可以使用 rowid 来确保匹配。
*/
merge into a_test_two a
using (select b.id, b.name, aa.rowid row_id from a_test_two aa, a_test_one b where aa.id = b.id) t
on (a.rowid = t.row_id)
when matched then
update set a.name = 'vip-name';
执行后的结果:
2、样例二:将一张表的一个字段更新为另外一张表的某个字段的值
将一张表的一个字段更新为另外一张表的某个字段的值,当然被 update 的值也可以是由另一个表中的数据运算而来。这里简单做一个例子。
2.1、使用方式一(update…set…where exists…)来实现
update
a_test_two a
set
# a表的某个字段等于某个结果集中的某个字段
name = (select name from a_test_one b where b.id = a.id) #这里只是一个示例,name它是可以等于b表的任意类型相同的字段
where exists (
select 1 from a_test_one b where b.id = a.id
)
/**
* 注意:
* where exists 的语句是不能省略的,否则会导致一些没有匹配的行会被更新成null值。
*/
执行后的结果:
2.1.1、注意事项
where exists(......)
的语句是不能缺失的,否则会导致一些没有匹配的行会被更新成null值。如下图所示:
2.2、使用方式二(merge into…when matched…)来实现
merge into a_test_two a
using (select b.id, b.name from a_test_one b) t
on (a.id = t.id)
when matched then
update set a.name = t.name; #这里只是一个示例,a表的name字段它是可以等于t(结果集)中的任意类型相同的字段
/**
* 看不懂的话,可以尝试着用英语的角度去读,然后大致通过英语单词来翻译一下,就知道是怎么回事了。
*
* 从英语的角度可以看出:
* merge into a_table a 合并于某一张表
* using ( 一个结果集 ) r 使用某一个结果集
* on a.xxx = r.xxx 条件
* when matched then 当条件匹配的时候
* update set a.column = r.column; 更新a表的字段为结果集中的某个字段
*/
/**
* 如果说表中没设置 ID 主键,没办法保证匹配条件的时候还是可以使用 rowid 来确保匹配。
*/
merge into a_test_two a
using (select b.id, b.name, aa.rowid row_id from a_test_two aa, a_test_one b where aa.id = b.id) t
on (a.rowid = t.row_id)
when matched then
update set a.name = t.name; #这里只是一个示例,a表的name字段它是可以等于t(结果集)中的任意类型相同的字段
执行后的结果:
3、样例三:将一张表的多个字段更新为另外一张表的某些字段的值
在实现业务需求中可能会有将一张表的多个字段更新为另外一张表的某些字段的值的情况出现,这里也做两个示例介绍一下。
3.1、使用方式一(update…set…where exists…)来实现
update
a_test_two a
set
(a.name,a.order_cost) # a表的字段等于某个结果集中的字段
=
(select b.name,b.order_cost from a_test_one b where b.id = a.id and b.age in (12,34))
where
exists
(select 1 from a_test_one b where b.id = a.id and b.age in (12,34));
/**
* 注意:
* where exists 的语句是不能省略的,否则会导致一些没有匹配的行会被更新成null值。
*/
执行后的结果:
3.1.1、注意事项
注意事项点击查看
3.2、使用方式二(merge into…when matched…)来实现
merge into a_test_two a
using (select b.id,b.name,b.order_cost from a_test_one b where b.age in (12,34)) t
on (a.id = t.id)
when matched then update
set a.name = t.name,
a.order_cost = t.order_cost; #这里只是一个示例,a表的字段它是可以等于t(结果集)中的任意类型相同的字段
/**
* 看不懂的话,可以尝试着用英语的角度去读,然后大致通过英语单词来翻译一下,就知道是怎么回事了。
*
* 从英语的角度可以看出:
* merge into a_table a 合并于某一张表
* using ( 一个结果集 ) r 使用某一个结果集
* on a.xxx = r.xxx 条件
* when matched then 当条件匹配的时候
* update set a.column = r.column; 更新a表的字段为结果集中的字段
*/
/**
* 如果说表中没设置 ID 主键,没办法保证匹配条件的时候还是可以使用 rowid 来确保匹配。
*/
merge into a_test_two a
using (select b.id, b.name,b.order_cost,aa.rowid row_id from a_test_two aa, a_test_one b where aa.id = b.id) t
on (a.rowid = t.row_id)
when matched then update
set a.name = t.name
a.order_cost = t.order_cost; #这里只是一个示例,a表的字段它是可以等于t(结果集)中的任意类型相同的字段
执行后的结果: