Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Truy vấn MySQL lấy giá trị trường không chứa khoảng trống?

Sử dụng NOT LIKE cho việc này. Trước hết, hãy tạo một bảng:

mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFullName varchar(40) ); Query OK, 0 rows affected (0.66 sec)

Chèn các bản ghi vào bảng bằng lệnh 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)

Hiển thị tất cả các bản ghi từ bảng bằng câu lệnh select:

mysql> select * from DemoTable;

Điều này sẽ tạo ra đầu ra sau:

+----+-----------------+
| Id | StudentFullName |
+----+-----------------+
| 1  | JohnSmith       |
| 2  | John Doe        |
| 3  | Adam Smith      |
| 4  | CarolTaylor     |
+----+-----------------+
4 rows in set (0.00 sec)

Truy vấn sau để lấy giá trị trường không chứa khoảng trống:

mysql> select * from DemoTable where StudentFullName NOT LIKE '% %';

Điều này sẽ tạo ra đầu ra sau:

+----+-----------------+
| Id | StudentFullName |
+----+-----------------+
| 1  | JohnSmith       |
| 4  | CarolTaylor     |
+----+-----------------+
2 rows in set (0.18 sec)