Home  >  Article  >  Database  >  Set similar values ​​for columns in MySQL table?

Set similar values ​​for columns in MySQL table?

PHPz
PHPzforward
2023-09-02 09:17:08823browse

为 MySQL 表中的列设置类似的值?

You can set values ​​for all records in a column with the help of the update command.

If you want to set NULL values ​​for all records in a column, the syntax is as follows -

update yourTableName set yourColumnName = NULL;

Or, if you want to use an empty string, the syntax is as follows -

update yourTableName set yourColumnName = ’’;

To understand the above concept, let us create a table. Query to create table.

mysql> create table StudentDemo
   −> (
   −> Studentid int,
   −> StudentName varchar(100),
   −> Age int
   −> );
Query OK, 0 rows affected (0.64 sec)

The following is the table into which records are inserted-

mysql> insert into StudentDemo values(1,'Johnson',23);
Query OK, 1 row affected (0.18 sec)

mysql> insert into StudentDemo values(2,'Carol',24);
Query OK, 1 row affected (0.16 sec)

mysql> insert into StudentDemo values(3,'David',20);
Query OK, 1 row affected (0.18 sec)

mysql> insert into StudentDemo values(4,'Bob',21);
Query OK, 1 row affected (0.19 sec)

Use the select statement to display all records in the table-

mysql> select *from StudentDemo;

The following is the output-

+-----------+-------------+------+
| Studentid | StudentName | Age |
+-----------+-------------+------+
|         1 | Johnson     | 23   |
|         2 | Carol       | 24   |
|         3 | David       | 20   |
|         4 | Bob         | 21   |
+-----------+-------------+------+
4 rows in set (0.00 sec)

The following The query sets the column value to NULL for all records in a specific column. The query is as follows -

mysql> update StudentDemo set Age=NULL;
Query OK, 4 rows affected (0.14 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Now let us check it -

mysql> select *from StudentDemo;

The following output shows that we have successfully updated the "Age" column to NULL -

+-----------+-------------+------+
| Studentid | StudentName | Age |
+-----------+-------------+------+
|         1 | Johnson     | NULL |
|         2 | Carol       | NULL |
|         3 | David       | NULL |
|         4 | Bob         | NULL |
+-----------+-------------+------+
4 rows in set (0.00 sec)

The above is the detailed content of Set similar values ​​for columns in MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete