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

为 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 表中的列设置类似的值?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

上一篇 Python中使用Requests模块
下一篇 C# 程序使用 LINQ 根据薪水对部门为 ABC 的员工列表进行排序