如果 MySQL 列字段为空,是否将其视为 NULL?
mysqlmysqli database
首先我们创建一个表 −
mysql> create table DemoTable1362 -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(40) -> ); Query OK, 0 rows affected (0.67 sec)
使用 insert 命令在表中插入一些记录 −
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)
以下查询用于将列字段视为空值 −
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)