UPDATE 在不同資料庫中的使用方式

2022-08-26 13:27:21 字數 2200 閱讀 5932

mysql 中

update 表一 set gmoney = 表二.列名 from 表一,表二 where 表一.empid = 表二.empid

舉例:update table1 set table1.gmoney = table2.gmoney from table1,table2 where table1.empid = table2.empid

其中table1為第乙個表,table2為第二個表,table2.gmoney為你圖中的無列名那一列

一條update更新語句是不能更新多張表的,除非使用觸發器隱含更新。

而表的更新操作中,在很多情況下需要在表示式中引用要更新的表以外的資料。

sql server中

sql server提供了update的from子句,可以將要更新的表與其它的資料來源連線起來。

雖然只能對乙個表進行更新,但是通過將要更新的表與其它的資料來源連線起來,

就可以在update的表示式 中引用要更新的表以外的其它資料。

一般形式:

update a set 欄位1=b表字段表示式, 欄位2=b表字段表示式 from b where 邏輯表示式

舉例:update dbo.table2

set dbo.table2.colb = dbo.table2.colb + dbo.table1.colb

from dbo.table2

inner join dbo.table1

on (dbo.table2.cola = dbo.table1.cola);

實際更新的操作是在要更新的表上進行的,而不是在from子句所形成的新的結果集上進行的

oracle 中

oracle沒有update from語法,可以通過兩種實現方式:

1、利用子查詢:

update a set 欄位1=(select 字段表示式 from b where ...), 欄位2=(select 字段表示式 from b where ...)

where 邏輯表示式

update多個字段兩種寫法:

寫法一:

update table_1 a

set col_x1 = (select b.col_y1, b.col_y2 from table_2 b where b.col_n = a.col_m),

col_x2 = (select b.col_y2 from table_2 b where b.col_n = a.col_m)

where exists (select * from table_2 b where b.col_n = a.col_m)

或update table_1 a

set col_x1 = (select b.col_y1, b.col_y2 from table_2 b where b.col_n = a.col_m),

col_x2 = (select b.col_y2 from table_2 b where b.col_n = a.col_m)

where a.col_m=(select b.col_n from table_2 b where b.col_n = a.col_m)

寫法二:

update table_1 a

set (col_x1, col_x2) = (select b.col_y1, b.col_y2 from table_2 b where b.col_n = a.col_m)

where exists (select * from table_2 b where b.col_n = a.col_m);

或update table_1 a

set (col_x1, col_x2) = (select b.col_y1, b.col_y2 from table_2 b where b.col_n = a.col_m)

where a.col_m=(select b.col_n from table_2 b where b.col_n = a.col_m)

注意:1. 對於子查詢的值只能是乙個唯一值,不能是多值。

2. 子查詢在絕大多數情況下,最後面的where exists子句是重要的,否則將得到錯誤的結果。

且where  exists子句可用另一方法代替,如上。

最後的子句是對a表被更新記錄的限制,如無此句,對於a表中某記錄,如在b表中關聯不到對應的記錄,

則該記錄被更新欄位將被更新為null。where exists子句就是排除對a表中該情況的記錄進行更新。

在Visual C 中訪問不同資料庫

visual c 作是微軟極力推薦的下一代程式開發語言,他有乙個非常重要伴侶 net framework sdk,在他的裡面封裝了許多class library 類庫 visual c 要實現很多拓展功能,就必須借助於他的這個伴侶。在visual c 中對資料庫的處理是其功能的乙個重要表現。visu...

在Visual C 中訪問不同資料庫

visual c 作是微軟極力推薦的下一代程式開發語言,他有乙個非常重要伴侶 net framework sdk,在他的裡面封裝了許多class library 類庫 visual c 要實現很多拓展功能,就必須借助於他的這個伴侶。在visual c 中對資料庫的處理是其功能的乙個重要表現。visu...

Select Top在不同資料庫中的使用

select top在不同資料庫中的使用用法 1.oracle資料庫 select from table1 where rownum n 2.infomix資料庫 select first n from table1 3.db2資料庫 select row number over order by ...