Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

คิวรี่ 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)
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)

ข้อมูล

mysql> select * from DemoTable;
+----+-----------------+
| Id | StudentFullName |
+----+-----------------+
| 1  | JohnSmith       |
| 2  | John Doe        |
| 3  | Adam Smith      |
| 4  | CarolTaylor     |
+----+-----------------+

กรองไม่มีช่องว่าง

mysql> select * from DemoTable where StudentFullName NOT LIKE '% %';
+----+-----------------+
| Id | StudentFullName |
+----+-----------------+
| 1  | JohnSmith       |
| 4  | CarolTaylor     |
+----+-----------------+

รูปแบบ % % หมายถึงมีช่องว่างอย่างน้อย 1 ตัวตรงกลาง