Mysql增刪改查之更新紀錄

更新記錄

更新資料我們已經說過。需要修改內容,修改銀行卡餘額,修改裝備資訊的時候都需要使用到update,修改語句。

修改(也叫更新)語句的基本語語法如下:

##基本語法範例範例說明
#類別詳細解示
update 表格名稱set 欄位1=值1,欄位2=值2,欄位n=值n where 條件
update money set  balance=balance-500 where userid = 15;
#修改money表,將balance餘額減500。要求userid為15

假設我們有下面這一個表,表結構如下:usernamebalance王強
userid
#1
50000.00
##2######黃曉明######150000000.00############15#######150000000.00############15######馬雲######15000.00## ##########16######陳赫######1234131.00############

mysql> select * from emp where deptno=15;
+--------+------------+----------+
|用戶 ID |用戶名|  平衡|
+------+----------+----------+
| 15 | 15  馬雲    | 15000.00 |
+------+--------+-------------+
# 集合中的1 行(0.00 秒)

使用update語句進行記錄更新

mysql>  更新資金集餘額=餘額-500,其中使用者 ID = 15;
 查詢正常,1 行受影響(0.35 秒)
 符合的行:1 更改:1 警告:0

mysql> select * from emp where deptno=15;
 +------+----------+----------+
 |用戶 ID |用戶名|  平衡|
 +------+----------+----------+
 | 15 | 15  馬雲    | 14500.00 |
 +------+--------+-------------+
 1 row in set (0.00 sec)

##修改多個欄位

##mysql>更新資金設定  balance=balance-500,username='李文凱' where userid = 15;
 查詢正常,1 行受影響(0.00 秒)

 符合的行:1 更改:1 警告:0

mysql> select * from emp where deptno=15;

 +------+----------+----------+

 |用戶 ID |用戶名|  平衡|
 +------+----------+----------+
 | 15   |王寶強    | 14500.00 |
 +------+--------+-------------+
 1 row in set (0.00 sec)

同時對兩個表格進行更新

類別##基本語法update 表1,表2 set 欄位1=值1, 欄位2=值2, 欄位n=值n 其中條件範例update Money m,user u m.balance=m.balance*u.age where m.userid=u.id;範例說明修改money,將money表的別名設為m;user表的別名設為u;將m表的餘額改為m表的餘額*使用者表執行條件是:m.userid = u.idmysql> 的年齡。更新資金 m,用戶 u m.balance=m.balance*u.age where m.userid=u.id;
詳細解圖

繼續學習
||
<?php echo "Hello Mysql"; ?>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!