MySQL 查询获取不包含空格的字段值?

mysqlmysqli database更新于 2025/9/14 1:48:17

为此使用 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)

相关文章


有用资源