SQL中如何使用UPDATE语句进行联表更新(转)
在本例中: 我们要用表member中的name,age字段数据去更新user中的同字段名的数据,条件是当user 中的id字段值与member中的id字段值相等时进行更新.
SQL Server语法: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 ] ) ]
SQL Server示例: update a set a.name=b.name,a.age=b.age from user a,member b where a.id=b.id
Oracle语法: UPDATE updatedtable SET (col_name1[,col_name2...])= (SELECT col_name1,[,col_name2...] FROM srctable [WHERE where_definition])
Oracel 示例_1: update user a set (a.name, a.age)= (select b.name, b.age from member b where a.id=b.id)
Oracel 示例_2:
( 查询出HX_CASH_PRECONTRACT表中custname为NULL的记录,并将HX_FINANCE_ACCOUNT表中的custname的值赋予该字段)
UPDATE hx_cash_precontract a SET (a.custname) = (SELECT b.custname FROM hx_finance_account b WHERE a.pk_finance_account = b.pk_finance_account) WHERE a.custname IS NULL
MySQL语法: UPDATE table_references SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition]
MySQL 示例: update user a, member b set a.name= b.name, a.age= b.age where a.id=b.id