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

ใช้ HAVING LENGTH() กรองความยาวสตริงใน MySQL

ใช้ GROUP BY LENGTH(column) HAVING LENGTH(column) < n กรองกลุ่มตามความยาว

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

mysql> create table DemoTable ( Title text ); 
Query OK, 0 rows affected (0.39 sec)

mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('Introduction to Java'); Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('Introduction to SQL'); Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('Introduction to Python'); Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('Introduction to SQL'); Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('Introduction to Java'); Query OK, 1 row affected (0.13 sec)

คิวรีกรองความยาวน้อยกว่า 21 ตัวอักษร

mysql> select * from DemoTable 
group by length(Title) 
having length(Title) < 21;
+----------------------+
| Title                |
+----------------------+
| Introduction to SQL  |
| Introduction to Java |
+----------------------+
2 rows in set (0.00 sec)

ความยาว: 'Introduction to SQL' = 19, 'Introduction to Java' = 20, 'Introduction to MySQL' = 21, 'Introduction to Python' = 22