【SQL】sql update 多表關聯更新方法總結

#表結構:

一、表一:Test1

Id name age
1    
2    

二、表二:Test2

Id name age
1 小明 10
2 小紅 8

 

#實現將表Test2的name和age字段數據更新到表Test1中,按照id相等的條件

一、SQLServer多表更新方法:

語法:html

UPDATE { table_name WITH ( < table_hint_limited > [ ...n ] ) | view_name | rowset_function_limited } SET { column_name = { expression | DEFAULT | NULL } | @variable = expression | @variable = column = expression } [ ,...n ] { { [ FROM { < table_source > } [ ,...n ] ] [ WHERE < search_condition > ] } | [ WHERE CURRENT OF { { [ GLOBAL ] cursor_name } | cursor_variable_name } ] } [ OPTION ( < query_hint > [ ,...n ] ) ]

 例子:express

update test1 set test1.name=test2.name,test1.age=test2.age from test1 inner join test2 on test1.id=test2.id

二、Oracle 多表更新方法:

語法:spa

UPDATE updatedtable SET (col_name1[,col_name2...])= (SELECT col_name1,[,col_name2...] 
FROM srctable [WHERE where_definition])

例子:code

update test1 set (test1.name,test1.age)= (select test2.name,test2.age from test2 where test2.id=test1.id)

三、MySql多表更新方法:

語法:htm

UPDATE table_references SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition]

例子:blog

update test1,test2 set test1.name=test2.name,test1.age=test2.age where test1.id=test2.id

四、通用方法:(*^__^*) 

update test1 set name=(select name from test2 where test2.id=test1.id), age=(select age from test2 where test2.id=test1.id)

———————————————————————————————————————————————————ci

原文出處:https://www.cnblogs.com/willingtolove/p/10792713.htmlit

相關文章
相關標籤/搜索