想要為 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)

更新於:2019 年 7 月 30 日

59 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.