談談資料庫更新 Update語句 查詢

2021-04-14 01:17:21 字數 1252 閱讀 9577

談談資料庫更新(update語句)查詢

今天有人在群上問了關於資料庫更新的問題,在此,我將資料庫更新的問題給總結一下

說白了,資料庫更新就一種方法update,

其標準格式:update 表名 set 字段=值 where 條件

不過根據資料的**不同,還是有所區別的

1.從外部輸入

這種比較簡單

例:update tb set username="***xx" where userid="aasdd"

2.一些內部變數,函式等,比如時間等

直接將函式賦值給字段

update tb set lastdate=date() where userid="aasdd"

4.將同一記錄的乙個字段賦值給另乙個字段

update tb set lastdate= regdate where ***

5.將乙個表中的一批記錄更新到另外乙個表中

table1

id f1 f2

table2

id f1 f2

先要將table2中的f1 f2 更新到table1(相同的id)

update table1,table2 set table1.f1=table2.f1,table1.f2=table2.f2 where table1.id=table2.id

6.將同乙個表中的一些記錄更新到另外一些記錄中

表:aid   month   e_id     price

1       1           1        2

2       1           2        4

3       2           1         5

4       2           2        5

先要將表中2月份的產品price更新到1月份中

顯然,要找到2月份中和1月份中id相同的e_id並更新price到1月份中

這個完全可以和上面的方法來處理,不過由於同一表,為了區分兩個月份的,應該將表重新命名一下

update a,a as b set a.price=b.price where a.e_id=b.e_id and a.month=1 and b.month=2

當然,這裡也可以先將2月份的查詢出來,在用5.的方法去更新

update a,(select * from a where month=2)as b set a.price=b.price where a.e_id=b.e_id and a.month=1 

資料庫學習 update(批量更新)

資料更新update命令 用指定要求的值更新指定表中滿足天劍的資料的指定列的值 語法形式 update 表名 set 列名 表示式 子查詢 列名 表示式 子查詢 where 條件表示式 示例 1 將所有教師工資上調 10 原表資料 執行兩次後結果 2 將所有計算機學院的老師工資上調 10 updat...

談談資料庫sql語句

1 sql structured query language 2 建立表 create table 表名 column1 datatype length column2 datatype length 3 插入語句 insert into table field1,field2 values va...

update 更新語句

update 語句用於修改表中的資料。update 表名稱 set 列名稱 新值 where 列名稱 某值 lastname firstname address city gates bill xuanwumen 10 beijing wilson champs elysees 我們為 lastna...