如果 MySQL 列字段为空,是否将该字段视为 NULL?
我们首先创建一个表格 -
mysql> create table DemoTable1362 -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(40) -> ); Query OK, 0 rows affected (0.67 sec)
使用插入命令在表格中插入一些记录 -
mysql> insert into DemoTable1362(ClientName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1362(ClientName) values(' '); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1362(ClientName) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1362(ClientName) values(' '); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1362(ClientName) values('David'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1362(ClientName) values(NULL); Query OK, 1 row affected (0.07 sec)
使用 select 语句从表格中显示所有记录 -
mysql> select * from DemoTable1362;
这将生成以下输出 -
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 1 | Chris | | 2 | | | 3 | Bob | | 4 | | | 5 | David | | 6 | NULL | +----------+------------+ 6 rows in set (0.00 sec)
以下是将列字段视为 null 的查询(如果该字段为空)-
mysql> update DemoTable1362 -> set ClientName=NULL -> where ClientName='' or length(ClientName)=0; Query OK, 2 rows affected (0.21 sec) Rows matched: 2 Changed: 2 Warnings: 0
让我们再次检查表格记录 -
mysql> select * from DemoTable1362;
这将生成以下输出 -
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 1 | Chris | | 2 | NULL | | 3 | Bob | | 4 | NULL | | 5 | David | | 6 | NULL | +----------+------------+ 6 rows in set (0.00 sec)
广告