使用 AND 運算子執行 MySQL 更新


我們首先建立一個表 -

mysql> create table DemoTable613 (Id int,Age int,isMarried tinyint(1));
Query OK, 0 rows affected (0.54 sec)

使用 insert 命令插入某些記錄到表中 -

mysql> insert into DemoTable613 values(100,29,0);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable613 values(200,22,0);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable613 values(300,30,1);
Query OK, 1 row affected (0.19 sec)

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

mysql> select *from DemoTable613;

這會產生以下輸出 -

+------+------+-----------+
| Id   | Age  | isMarried |
+------+------+-----------+
|  100 |   29 |         0 |
|  200 |   22 |         0 |
|  300 |   30 |         1 |
+------+------+-----------+
3 rows in set (0.00 sec)

以下是查詢以使用 AND 運算子執行 MySQL 更新 -

mysql> update DemoTable613 set isMarried=(Age=29 AND (Id=100));
Query OK, 2 rows affected (0.24 sec)
Rows matched: 3 Changed: 2 Warnings: 0

我們再次檢查表記錄 -

mysql> select *from DemoTable613;

這會產生以下輸出 -

+------+------+-----------+
| Id   | Age  | isMarried |
+------+------+-----------+
|  100 |   29 |         1 |
|  200 |   22 |         0 |
|  300 |   30 |         0 |
+------+------+-----------+
3 rows in set (0.00 sec)

更新時間: 2019-08-22

90 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始吧
廣告
© . All rights reserved.