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

PHPz
PHPz 转载
2023-09-02 09:17:08 441浏览

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

您可以借助 update 命令为一列所有记录设置值。

如果您想为一列中的所有记录设置 NULL 值,语法如下 -

update yourTableName set yourColumnName = NULL;

或者,如果您想使用空字符串,则语法如下 -

update yourTableName set yourColumnName = ’’;

为了理解上述概念,让我们创建一个表。创建表的查询。

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

下面是插入记录的表 -

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)

借助 select 语句显示表中的所有记录 -

mysql> select *from StudentDemo;

以下是输出 -

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

以下查询将特定列中的所有记录的列值设置为 NULL。查询如下 -

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

现在让我们检查一下 -

mysql> select *from StudentDemo;

以下输出显示我们已成功将“Age”列更新为 NULL -

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

以上就是为 MySQL 表中的列设置类似的值?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除