使用單一查詢將 MySQL 表的所有列設定為特定值


我們首先建立一個表 −

mysql> create table DemoTable
(
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(40),
   ClientAge int,
   ClientCountryName varchar(40)
);
Query OK, 0 rows affected (0.57 sec)

使用 insert 命令向表中插入一些記錄 −

mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('Chris',25,'US');
Query OK, 1 row affected (0.33 sec)
mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('Bob',55,'UK');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('David',45,'AUS');
Query OK, 1 row affected (0.14 sec)

使用 select 語句在表中顯示所有記錄 −

mysql> select *from DemoTable;

這將產生以下輸出 −

+----------+------------+-----------+-------------------+
| ClientId | ClientName | ClientAge | ClientCountryName |
+----------+------------+-----------+-------------------+
|        1 | Chris      | 25        | US                |
|        2 | Bob        | 55        | UK                |
|        3 | David      | 45        | AUS               |
+----------+------------+-----------+-------------------+
3 rows in set (0.00 sec)

以下是將 MySQL 表的所有列設定為特定值的一個查詢——

mysql> update DemoTable
   set ClientName='Sam',ClientAge=48,ClientCountryName='AUS' where ClientId=2;
Query OK, 1 row affected (0.22 sec)
Rows matched : 1 Changed : 1 Warnings : 0

讓我們再次檢查表記錄 −

mysql> select *from DemoTable;

這將產生以下輸出 −

+----------+------------+-----------+-------------------+
| ClientId | ClientName | ClientAge | ClientCountryName |
+----------+------------+-----------+-------------------+
|        1 | Chris      |        25 | US                |
|        2 | Sam        |        48 | AUS               |
|        3 | David      |        45 | AUS               |
+----------+------------+-----------+-------------------+
3 rows in set (0.00 sec)

更新於: 10-Oct-2019

83 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.