为 MySQL 表中的列设置类似的值?
mysqlmysqli database
您可以借助 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)