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

คิวรี่ MySQL หาเรคคอร์ดที่มีความยาวน้อยกว่า X ตัวอักษร

ใช้ CHAR_LENGTH() ใน WHERE กรองความยาวสตริง

สร้างตารางและเพิ่มข้อมูล

mysql> create table DemoTable ( FullName varchar(50) ); 
Query OK, 0 rows affected (1.75 sec)
mysql> insert into DemoTable values('Chris Brown'); 
Query OK, 1 row affected (0.40 sec) 
mysql> insert into DemoTable values('David Miller'); 
Query OK, 1 row affected (0.91 sec) 
mysql> insert into DemoTable values('Robert Miller'); 
Query OK, 1 row affected (0.26 sec) 
mysql> insert into DemoTable values('John Smith'); 
Query OK, 1 row affected (0.89 sec)

คิวรี่หาชื่อที่ยาวน้อยกว่า 12 ตัวอักษร

mysql> select * from DemoTable where char_length(FullName) < 12;

ผลลัพธ์

+-------------+ 
| FullName    | 
+-------------+ 
| Chris Brown | 
| John Smith  | 
+-------------+ 
2 rows in set (0.00 sec)

Chris Brown = 11 ตัวอักษร | John Smith = 10 ตัวอักษร | David Miller = 12 (ไม่ผ่าน) | Robert Miller = 13 (ไม่ผ่าน) | ใช้ CHAR_LENGTH() นับตัวอักษร | LENGTH() นับไบต์