本文转载自http://wing123.iteye.com/blog/756273
在开发中,数据库来回换,而有些关键性的语法又各不相同,这是一件让开发人员很头痛的事情.
本文总结了Update语句更新多表时在SQL Server,Oracle,MySQL三种数据库中的用法.
在本例中: 我们要用表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
在开发中,数据库来回换,而有些关键性的语法又各不相同,这是一件让开发人员很头痛的事情.
本文总结了Update语句更新多表时在SQL Server,Oracle,MySQL三种数据库中的用法.
在本例中: 我们要用表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