多表關(guān)聯(lián)同時(shí)更新多條不同的記錄方法分享
1.首先創(chuàng)建兩張臨時(shí)表并錄入測(cè)試數(shù)據(jù):
create table #temptest1
(
id int,
name1 varchar(50),
age int
)
create table #temptest2
(
id int,
name1 varchar(50),
age int
)
查詢出此時(shí)的表數(shù)據(jù)為:
#temptest1 #temptest2
2.現(xiàn)在要將#temptest2中的年齡更新到相應(yīng)的#temptest1中的年齡。
其實(shí)就是讓[表1]中ID為1的年齡改成19,同時(shí)ID為2的年齡改成20。
當(dāng)然這里的要求是只用一句SQL,不能用循環(huán)。
結(jié)果如下:
實(shí)現(xiàn)方法如下:
Update t1
Set t1 .age = t2.age
From #temptest1 t1
Join #temptest2 t2
On t1.id = t2.id
(補(bǔ)充)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。
如何一次性更新多條不同值的記錄
標(biāo)題可能沒說(shuō)清楚,假設(shè)有這樣兩張表:
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)
)
現(xiàn)有記錄
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來(lái)對(duì)應(yīng)。
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行。
已用時(shí)間: 00: 00: 00.01
執(zhí)行計(jì)劃
----------------------------------------------------------
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)
統(tǒng)計(jì)信息
----------------------------------------------------------
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行。
已用時(shí)間: 00: 00: 00.00
執(zhí)行計(jì)劃
----------------------------------------------------------
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)
統(tǒng)計(jì)信息
----------------------------------------------------------
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行。
已用時(shí)間: 00: 00: 00.01
執(zhí)行計(jì)劃
----------------------------------------------------------
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)
統(tǒng)計(jì)信息
----------------------------------------------------------
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
相關(guān)文章
SQL?Server忘記sa賬號(hào)密碼重新添加新管理賬號(hào)
這篇文章介紹了SQL?Server忘記sa賬號(hào)密碼重新添加新賬號(hào)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04關(guān)于SQL中CTE(公用表表達(dá)式)(Common Table Expression)的總結(jié)
WITH AS短語(yǔ),也叫做子查詢部分(subquery factoring),可以讓你做很多事情,定義一個(gè)SQL片斷,該SQL片斷會(huì)被整個(gè)SQL語(yǔ)句所用到2012-08-08SQL 多條件查詢幾種實(shí)現(xiàn)方法詳細(xì)介紹
這篇文章主要介紹了SQL 多條件查詢兩種實(shí)現(xiàn)方法詳細(xì)介紹的相關(guān)資料,一種是排列結(jié)合,另一種是動(dòng)態(tài)拼接SQL,需要的朋友可以參考下2016-12-12SQL Server設(shè)置主鍵自增長(zhǎng)列(使用sql語(yǔ)句實(shí)現(xiàn))
主鍵自增長(zhǎng)列在進(jìn)行數(shù)據(jù)插入的時(shí)候,很有用的,如可以獲取返回的自增ID值,接下來(lái)將介紹SQL Server如何設(shè)置主鍵自增長(zhǎng)列,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01SQLServer EVENTDATA()函數(shù)來(lái)獲取DDL 觸發(fā)器信息
SQL Server 2005/2008中可以使用EVENTDATA函數(shù)來(lái)獲取DDL觸發(fā)器的上下文,從而在ROLLBACK之前截獲DDL信息。EVENTDATA返回XML字段,下面的例子顯示如何截獲Drop Table的DDL信息。2009-07-07數(shù)據(jù)庫(kù)更新Sqlserver腳本總結(jié)
數(shù)據(jù)庫(kù)更新Sqlserver腳本總結(jié),需要的朋友可以參考下。2011-06-06VS2022與SQL?server數(shù)據(jù)庫(kù)連接與訪問方法操作
在學(xué)習(xí)過(guò)程中我們常常需要連接數(shù)據(jù)庫(kù)對(duì)大量的數(shù)據(jù)進(jìn)行管理,下面這篇文章主要給大家介紹了關(guān)于VS2022與SQL?server數(shù)據(jù)庫(kù)連接與訪問的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01SQL Server 數(shù)據(jù)庫(kù)分離與附加(圖文教程)
SQL Server 數(shù)據(jù)庫(kù)分離與附加(圖文教程),需要的朋友可以參考一下2013-05-05