位置: 编程技术 - 正文
查询出此时的表数据为:
#temptest1 #temptest2
推荐整理分享多表关联同时更新多条不同的记录方法分享(多表关联执行顺序),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:多表关联添加,多表关联执行顺序,多表关联查询还会走索引吗,多表关联更新数据,多表关联添加,多表关联添加,多表关联怎么优化,多表关联join,内容如对您有帮助,希望把文章链接给更多的朋友!
2.现在要将#temptest2中的年龄更新到相应的#temptest1中的年龄。
其实就是让[表1]中ID为1的年龄改成,同时ID为2的年龄改成。
当然这里的要求是只用一句SQL,不能用循环。
结果如下:
实现方法如下:
Update t1
Set t1 .age = t2.age
From #temptest1 t1
Join #temptest2 t2
On t1.id = t2.id
(补充)Sql Server Merge命令写法:
merge into #temptest1 t1 using(select age,id from #temptest2) t2on t1.id = t2.idwhen matched thenupdate set t1.age = t2.age
是不是挺有趣的Sql。如何一次性更新多条不同值的记录标题可能没说清楚,假设有这样两张表: 现有记录 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行。 已用时间: : : . 执行计划 ---------------------------------------------------------- Plan hash value: -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | UPDATE STATEMENT | | 5 | | ()| :: | | 1 | UPDATE | TA | | | | | |* 2 | HASH JOIN SEMI | | 5 | | 5 ()| :: | | 3 | TABLE ACCESS FULL | TA | 5 | | 2 (0)| :: | | 4 | VIEW | VW_SQ_1 | 4 | | 2 (0)| :: | | 5 | TABLE ACCESS FULL| TB | 4 | | 2 (0)| :: | |* 6 | TABLE ACCESS FULL | TB | 1 | | 2 (0)| :: | -------------------------------------------------------------------------------- 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 consistent gets 0 physical reads redo size bytes sent via SQL*Net to client 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行。 已用时间: : : . 执行计划 ---------------------------------------------------------- Plan hash value: ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | UPDATE STATEMENT | | 1 | | 7 ()| :: | | 1 | UPDATE | TA | | | | | |* 2 | FILTER | | | | | | | 3 | TABLE ACCESS FULL| TA | 5 | | 2 (0)| :: | |* 4 | TABLE ACCESS FULL| TB | 1 | | 2 (0)| :: | |* 5 | TABLE ACCESS FULL | TB | 1 | | 2 (0)| :: | ---------------------------------------------------------------------------- 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) 统计信息 ---------------------------------------------------------- recursive calls 1 db block gets consistent gets 0 physical reads redo size bytes sent via SQL*Net to client 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行。 已用时间: : : . 执行计划 ---------------------------------------------------------- Plan hash value: ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | UPDATE STATEMENT | | 4 | | 5 ()| :: | | 1 | UPDATE | TA | | | | | |* 2 | HASH JOIN | | 4 | | 5 ()| :: | | 3 | TABLE ACCESS FULL| TB | 4 | | 2 (0)| :: | | 4 | TABLE ACCESS FULL| TA | 5 | | 2 (0)| :: | ---------------------------------------------------------------------------- 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 consistent gets 0 physical reads redo size bytes sent via SQL*Net to client bytes received via SQL*Net from client 3 SQL*Net roundtrips to/from client 3 sorts (memory) 0 sorts (disk) 4 rows processed
在数据库中自动生成编号的实现方法分享 在使用数据库的时候,难免要在使用过程中进行删除的操作,如果是使用int类型的字段,令其自增长,这是个最简单的办法,但是后果会有些不是你想要的!看
运行asp.net程序 报错:磁盘空间不足 一看,C盘只有不到2M可用空间,一查原因,sqlserver安装路径下的log目录文件占了好大,5G多,于是上网搜了下,解决了:把与sqlserver有关的服务停掉,log
MSSQL优化之探索MSSQL执行计划(转) 网上的SQL优化的文章实在是很多,说实在的,我也曾经到处找这样的文章,什么不要使用IN了,什么OR了,什么AND了,很多很多,还有很多人拿出仅几S
标签: 多表关联执行顺序
本文链接地址:https://www.jiuchutong.com/biancheng/348888.html 转载请保留说明!友情链接: 武汉网站建设