您可以借助 update 命令為一列所有記錄設(shè)置值。
如果您想為一列中的所有記錄設(shè)置 NULL 值,語法如下 –
update yourTableName set yourColumnName = NULL;
登錄后復(fù)制
或者,如果您想使用空字符串,則語法如下 –
update yourTableName set yourColumnName = ’’;
登錄后復(fù)制
為了理解上述概念,讓我們創(chuàng)建一個(gè)表。創(chuàng)建表的查詢。
mysql> create table StudentDemo −> ( −> Studentid int, −> StudentName varchar(100), −> Age int −> ); Query OK, 0 rows affected (0.64 sec)
登錄后復(fù)制
下面是插入記錄的表 –
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)
登錄后復(fù)制
借助 select 語句顯示表中的所有記錄 –
mysql> select *from StudentDemo;
登錄后復(fù)制登錄后復(fù)制
以下是輸出 –
+-----------+-------------+------+ | Studentid | StudentName | Age | +-----------+-------------+------+ | 1 | Johnson | 23 | | 2 | Carol | 24 | | 3 | David | 20 | | 4 | Bob | 21 | +-----------+-------------+------+ 4 rows in set (0.00 sec)
登錄后復(fù)制
以下查詢將特定列中的所有記錄的列值設(shè)置為 NULL。查詢?nèi)缦?–
mysql> update StudentDemo set Age=NULL; Query OK, 4 rows affected (0.14 sec) Rows matched: 4 Changed: 4 Warnings: 0
登錄后復(fù)制
現(xiàn)在讓我們檢查一下 –
mysql> select *from StudentDemo;
登錄后復(fù)制登錄后復(fù)制
以下輸出顯示我們已成功將“Age”列更新為 NULL –
+-----------+-------------+------+ | Studentid | StudentName | Age | +-----------+-------------+------+ | 1 | Johnson | NULL | | 2 | Carol | NULL | | 3 | David | NULL | | 4 | Bob | NULL | +-----------+-------------+------+ 4 rows in set (0.00 sec)
登錄后復(fù)制
以上就是為 MySQL 表中的列設(shè)置類似的值?的詳細(xì)內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!






