多表关联同时更新多条不同的记录方法分享

以下为测试例子。

1.首先创建两张临时表并录入测试数据:

复制代码 代码如下:

create table #temptest1

(

id int,

name1 varchar(50),

age int

)

create table #temptest2

(

id int,

name1 varchar(50),

age int

)

查询出此时的表数据为:

#temptest1                 #temptest2

2.现在要将#temptest2中的年龄更新到相应的#temptest1中的年龄。

其实就是让[表1]中ID为1的年龄改成19,同时ID为2的年龄改成20。

当然这里的要求是只用一句SQL,不能用循环。

结果如下:

实现方法如下:

Update t1

Set t1 .age = t2.age

From  #temptest1 t1

Join #temptest2 t2

On  t1.id = t2.id

(补充)Sql Server 2008 Merge命令写法:

merge into #temptest1 t1
using(select age,id from #temptest2) t2
on t1.id = t2.id
when matched then
update set t1.age = t2.age

是不是挺有趣的Sql。

如何一次性更新多条不同值的记录
标题可能没说清楚,假设有这样两张表:
复制代码 代码如下:
create table testA(
id number,
eng varchar2(3),
chi varchar2(3)
)
create table testB(
id number,
eng varchar2(3),
chi varchar2(3),
anythingother varchar2(1)
)

现有记录
testA:
ID ENG CHI
===============
1 a 一
2 b 二
3 c 三
testB:
ID ENG CHI ANY....
=================
1 d 四
2 e 五
3 f 六
我想把testB中的记录的ENG,CHI字段更新到testA中去,以ID来对应。

CODE:

SQL> set autot on
SQL> update ta set ta.b=(select tb.b from tb where ta.a=tb.a) where exists (select 1 from tb where ta.a=tb.a);
已更新4行。
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 1137212925
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 5 | 165 | 20 (30)| 00:00:01 |
| 1 | UPDATE | TA | | | | |
|* 2 | HASH JOIN SEMI | | 5 | 165 | 5 (20)| 00:00:01 |
| 3 | TABLE ACCESS FULL | TA | 5 | 100 | 2 (0)| 00:00:01 |
| 4 | VIEW | VW_SQ_1 | 4 | 52 | 2 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL| TB | 4 | 52 | 2 (0)| 00:00:01 |
|* 6 | TABLE ACCESS FULL | TB | 1 | 26 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("TA"."A"="ITEM_1")
6 - filter("TB"."A"=:B1)
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
0 recursive calls
4 db block gets
23 consistent gets
0 physical reads
1004 redo size
840 bytes sent via SQL*Net to client
856 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
4 rows processed
SQL> update ta set ta.b=(select tb.b from tb where ta.a=tb.a) where ta.a= (select tb.a from tb where ta.a=tb.a);
已更新4行。
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 3571861550
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 1 | 20 | 7 (15)| 00:00:01 |
| 1 | UPDATE | TA | | | | |
|* 2 | FILTER | | | | | |
| 3 | TABLE ACCESS FULL| TA | 5 | 100 | 2 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL| TB | 1 | 13 | 2 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL | TB | 1 | 26 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("TA"."A"= (SELECT "TB"."A" FROM "TB" "TB" WHERE
"TB"."A"=:B1))
4 - filter("TB"."A"=:B1)
5 - filter("TB"."A"=:B1)
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
11 recursive calls
1 db block gets
53 consistent gets
0 physical reads
588 redo size
840 bytes sent via SQL*Net to client
858 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
4 rows processed

如果 create unique index tb_a_uidx on tb(a);

[Copy to clipboard] [ - ]

CODE:

SQL> update (select ta.b tab1 ,tb.b tbb from ta,tb where ta.a=tb.a) set tab1=tbb;
已更新4行。
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 1761655026
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 4 | 184 | 5 (20)| 00:00:01 |
| 1 | UPDATE | TA | | | | |
|* 2 | HASH JOIN | | 4 | 184 | 5 (20)| 00:00:01 |
| 3 | TABLE ACCESS FULL| TB | 4 | 104 | 2 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| TA | 5 | 100 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("TA"."A"="TB"."A")
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
8 recursive calls
4 db block gets
17 consistent gets
0 physical reads
1004 redo size
840 bytes sent via SQL*Net to client
827 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
4 rows processed

时间: 2024-07-30 19:23:37

多表关联同时更新多条不同的记录方法分享的相关文章

多表关联同时更新多条不同的记录方法分享_MsSql

以下为测试例子. 1.首先创建两张临时表并录入测试数据: 复制代码 代码如下: create table #temptest1 ( id int, name1 varchar(50), age int ) create table #temptest2 ( id int, name1 varchar(50), age int ) 查询出此时的表数据为: #temptest1                 #temptest2       2.现在要将#temptest2中的年龄更新到相应的#t

MySql多表关联Update更新sql语句

对单表执行更新没有什么好说的,无非就是update table_name set col1 = xx,col2 = yy where col = zz,主要就是where条件的设置.有时候更新某个表可能会涉及到多张数据表,例如:  代码如下 复制代码 update table_1 set score = score + 5 where uid in (select uid from table_2 where sid = 10); 其实update也可以用到left join.inner joi

sqlserver 多表关联时在where语句中慎用trim()方法

类似如下: select A.key,B.key,C.key from A,B,C where trim(A.key)=trim(B.fk) and trim(A.col)=trim(C.pk). 在主表A(200多条记录)关联附表B(4万多条记录)时用了1秒钟时间,该值在不同机器执行可能有所差异,但比不加trim速度稍微慢一些,但是不是特别明显. 其sql语句类似如下: select A.key,B.key from A,B where trim(A.key)=trim(B.fk) 但是,在上

sqlserver 多表关联时在where语句中慎用trim()方法_MsSql

类似如下: select A.key,B.key,C.key from A,B,C where trim(A.key)=trim(B.fk) and trim(A.col)=trim(C.pk). 在主表A(200多条记录)关联附表B(4万多条记录)时用了1秒钟时间,该值在不同机器执行可能有所差异,但比不加trim速度稍微慢一些,但是不是特别明显. 其sql语句类似如下: select A.key,B.key from A,B where trim(A.key)=trim(B.fk) 但是,在上

SQL update 多表关联更新的实现代码

实现多表更新,尤其是A表和A的子表B表数据更新,下面是例子 有A.B张表,其记录如下: A表 c1       c2 -------------- 1       a1 2       a2 3       a3 8       a8 B表 c1       c3 -------------- 1        b1 2        b1 3        b3 10      b10 A.c1与B.c1相等,用一条sql语句,实现A.c2的值更新为B.c3 -----------------

javaweb-探讨个问题:一对一双向关联的表,如何插入一条记录。

问题描述 探讨个问题:一对一双向关联的表,如何插入一条记录. 嗨,大家有空吗?探讨一个问题. 一对一双向关联,要插入一条记录. 举个简单例子.一个班级有一个班主任,一个老师只能任一个班级班主任.(一对一) Teacher表 Class表 id(自增) id(自增) name name classId teacherId 插入一条记录: 思路--> 第一步:先向Teacher插入一条记录.返回新插入的id值.(selectKey) 第二步:再向Class插入一条记录,teacherId=上一步返回

Oracle\MS SQL Server Update多表关联更新

原文:Oracle\MS SQL Server Update多表关联更新 一条Update更新语句是不能更新多张表的,除非使用触发器隐含更新.而表的更新操作中,在很多情况下需要在表达式中引用要更新的表以外的数据.我们先来讨论根据其他表数据更新你要更新的表   一.MS    SQL    Server   多表关联更新      sql server提供了update的from 子句,可以将要更新的表与其它的数据源连接起来.虽然只能对一个表进行更新,但是通过将要更新的表与其它的数据源连接起来,就

sqlserver 1 n-sqlserver 两个表关联1:n求随机取一条数据的sql语句实现!

问题描述 sqlserver 两个表关联1:n求随机取一条数据的sql语句实现! 现在要补齐tb1中演唱歌曲字段.条件是去tb2中查找相同艺人演唱过的歌曲,随机填充到tb1中的歌曲名字段 一个歌手不止演唱一首歌,所以tb2中是艺人演唱所有歌曲的集合.tb1中同一个歌手可能出现好几次 补齐时候需根据tb1中艺人名称去tb2也就是艺人歌曲汇总表中查找相同艺人演唱的歌曲名称. 需要在艺人名相同情况下随机取tb2中演唱歌曲名去一一补齐tb1中的字段 tb1 tb1 艺人 演唱歌曲名 a null b n

mysql表关联只取关联表中最近一条数据

问题描述 mysql表关联只取关联表中最近一条数据 表A{id,userId,logTime} 表B{id,userId,departmentId,updateTime} 表A为数据记录 表B相当于历史记录表(userId在updateTime时间之前的departmentId) 现在表A与表B关联 但是只能让表B中符合条件的最近一记录关联上 (也是updateTime在logTime之前并且最近的一条) 我是这么做的 SELECT A.id,B.userId,B.departmentId FR