How to set all values in a single column MySQL Query?

MySQLMySQLi Database

To set all values in a single column MySQL query, you can use UPDATE command.

The syntax is as follows.

update yourTableName set yourColumnName =yourValue;

To understand the above syntax, let us create a table. The query to create a table is as follows.

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)

Now you can insert some records in the table using insert command.

The query is as follows.

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)

Display all records from the table using select statement.

The query is as follows.

mysql> select *from setAllValuesDemo;

The following is the output.

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

Here is the query to set all values in a single column MySQL query.

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

Now check the table records once again using select statement.

The query is as follows.

mysql> select *from setAllValuesDemo;

The following is the output.

+----+-------+--------+
| Id | Name  | Amount |
+----+-------+--------+
| 1  | John  | 10500  |
| 2  | Carol | 10500  |
| 3  | Bob   | 10500  |
| 4  | David | 10500  |
+----+-------+--------+
4 rows in set (0.00 sec)
raja
Published on 29-Mar-2019 11:01:37
Advertisements