如何在單個 MySQL 查詢中設定單個列中的所有值?


想要在單個 MySQL 查詢中設定單個列中的所有值,可以使用 UPDATE 命令。

語法如下。

update yourTableName set yourColumnName =yourValue;

為了理解上述語法,我們先建立一個表。建立表的查詢如下。

mysql> create table setAllValuesDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20),
   -> Amount int
   -> );
Query OK, 0 rows affected (0.64 sec)

現在,可以使用 insert 命令在表中插入一些記錄。

查詢如下。

mysql> insert into setAllValuesDemo(Name,Amount) values('John',2345);
Query OK, 1 row affected (0.22 sec)
mysql> insert into setAllValuesDemo(Name,Amount) values('Carol',47586);
Query OK, 1 row affected (0.13 sec)
mysql> insert into setAllValuesDemo(Name,Amount) values('Bob',95686);
Query OK, 1 row affected (0.15 sec)
mysql> insert into setAllValuesDemo(Name,Amount) values('David',95667);
Query OK, 1 row affected (0.15 sec)

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

查詢如下。

mysql> select *from setAllValuesDemo;

輸出如下。

+----+-------+--------+
| Id | Name  | Amount |
+----+-------+--------+
| 1  | John  | 2345   |
| 2  | Carol | 47586  |
| 3  | Bob   | 95686  |
| 4  | David | 95667  |
+----+-------+--------+
4 rows in set (0.00 sec)

以下查詢可以設定單個 MySQL 查詢中單個列中的所有值。

mysql> update setAllValuesDemo set Amount=10500;
Query OK, 4 rows affected (0.20 sec)
Rows matched: 4 Changed: 4 Warnings: 0

現在,再次使用 select 語句檢查表記錄。

查詢如下。

mysql> select *from setAllValuesDemo;

輸出如下。

+----+-------+--------+
| Id | Name  | Amount |
+----+-------+--------+
| 1  | John  | 10500  |
| 2  | Carol | 10500  |
| 3  | Bob   | 10500  |
| 4  | David | 10500  |
+----+-------+--------+
4 rows in set (0.00 sec)

更新於: 2019 年 7 月 30 日

11K+ 人檢視

開啟您的 職業生涯

完成課程獲得認證

開始
廣告