如何在单列 MySQL 查询中设置所有值?

mysqlmysqli database更新于 2025/1/4 21:32:17

要设置单列 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)

相关文章


有用资源