用于获取不包含空值的字段值的 MySQL 查询?
为此使用 NOT LIKE。我们首先创建一个表 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFullName varchar(40) ); Query OK, 0 rows affected (0.66 sec)
使用 insert 命令在表中插入记录 -
mysql> insert into DemoTable(StudentFullName) values('JohnSmith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentFullName) values('John Doe'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentFullName) values('Adam Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentFullName) values('CarolTaylor'); Query OK, 1 row affected (0.19 sec)
使用 select 语句在表中显示所有记录 -
mysql> select * from DemoTable;
这将产生以下输出 -
+----+-----------------+ | Id | StudentFullName | +----+-----------------+ | 1 | JohnSmith | | 2 | John Doe | | 3 | Adam Smith | | 4 | CarolTaylor | +----+-----------------+ 4 rows in set (0.00 sec)
以下是用于获取不包含空值的字段值的查询 -
mysql> select *from DemoTable where StudentFullName NOT LIKE '% %';
这将产生以下输出 -
+----+-----------------+ | Id | StudentFullName | +----+-----------------+ | 1 | JohnSmith | | 4 | CarolTaylor | +----+-----------------+ 2 rows in set (0.18 sec)
广告