如何在单列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
   -> );

现在,您可以使用insert命令在表中插入一些记录。

查询如下。

mysql> insert into setAllValuesDemo(Name,Amount) values('John',2345);
mysql> insert into setAllValuesDemo(Name,Amount) values('Carol',47586);
mysql> insert into setAllValuesDemo(Name,Amount) values('Bob',95686);
mysql> insert into setAllValuesDemo(Name,Amount) values('David',95667);

使用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;
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)