透過追加使用者定義變數的值更新 MySQL 錶行資料列?


讓我們首先建立一個表 −

mysql> create table DemoTable1349
   -> (
   -> ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ProductPrice int
   -> );
Query OK, 0 rows affected (0.71 sec)

使用插入命令在表中插入一些記錄 -

mysql> insert into DemoTable1349(ProductPrice) values(7644);
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable1349(ProductPrice) values(90843);
Query OK, 1 row affected (0.06 sec)
mysql> insert into DemoTable1349(ProductPrice) values(9083);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1349(ProductPrice) values(10000);
Query OK, 1 row affected (0.12 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select * from DemoTable1349;

這將產生以下輸出 -

+-----------+--------------+
| ProductId | ProductPrice |
+-----------+--------------+
|         1 |          7644|
|         2 |         90843|
|         3 |          9083|
|         4 |         10000|
+-----------+--------------+
4 rows in set (0.00 sec)

以下是更新 MySQL 錶行資料的查詢。首先,我們設定了一個使用者定義的變數,我們將在稍後追加此變數 -

mysql> set @AppendValue:=500;
Query OK, 0 rows affected (0.00 sec)
mysql> update DemoTable1349 set ProductPrice=ProductPrice+@AppendValue where ProductId=4;
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0

讓我們再次檢查表記錄 -

mysql> select * from DemoTable1349;

這將產生以下輸出 -

+-----------+--------------+
| ProductId | ProductPrice |
+-----------+--------------+
|         1 |          7644|
|         2 |         90843|
|         3 |          9083|
|         4 |         10500|
+-----------+--------------+
4 rows in set (0.00 sec)

更新於: 2019 年 11 月 5 日

276 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.