7月2

sql 多表关联更新方法

| |
21:11数据库  From: 本站原创
#表结构:
1、表一:Test1
Id  name  age
1      
2      

2、表二:Test2
Id  name  age
1  小明  10
2  小红  8


#实现将表Test2的name和age字段数据更新到表Test1中,按照id相等的条件

1、SQLServer多表更新方法:
语法:

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

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

2、Oracle 多表更新方法:
语法:

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

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

3、MySql多表更新方法:
语法:

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

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

4、通用方法:(*^__^*)
update test1
set name=(select name from test2 where test2.id=test1.id),
age=(select age from test2 where test2.id=test1.id)

来源:Heck's Blog
地址:https://www.heckjj.com/post/542/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!
阅读(54) | 评论(0) | 引用(0)