Bạn có thể sử dụng SUBSTRING () từ MySQL để giới hạn độ dài của chuỗi. Cú pháp như sau
SELECT SUBSTRING(yourColumnName,1,yourIntegerValueToGetTheCharacters) as anyVariableName from yourTableName;
Để hiểu cú pháp trên, chúng ta hãy tạo một bảng. Truy vấn để tạo bảng như sau
mysql> create table limitLengthOfLongTextDemo -> ( -> sentence LONGTEXT -> ); Query OK, 0 rows affected (0.74 sec)
Chèn một số bản ghi trong bảng bằng lệnh chèn. Truy vấn như sau
mysql> insert into limitLengthOfLongTextDemo values('This is the introduction to MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into limitLengthOfLongTextDemo values('PL/SQL is the extension of Structured Query Language'); Query OK, 1 row affected (0.19 sec) mysql> insert into limitLengthOfLongTextDemo values('Java is an Object Oriented Programming Language'); Query OK, 1 row affected (0.20 sec)
Hiển thị tất cả các bản ghi từ bảng bằng cách sử dụng câu lệnh select. Truy vấn như sau
mysql> select *from limitLengthOfLongTextDemo;
Sau đây là kết quả
+------------------------------------------------------+ | sentence | +------------------------------------------------------+ | This is the introduction to MySQL | | PL/SQL is the extension of Structured Query Language | | Java is an Object Oriented Programming Language | +------------------------------------------------------+ 3 rows in set (0.00 sec)
Đây là truy vấn để lấy các ký tự của giá trị đã cho
mysql> select substring(sentence,1,26) as 26Characters from limitLengthOfLongTextDemo;
Sau đây là kết quả
+----------------------------+ | 26Characters | +----------------------------+ | This is the introduction t | | PL/SQL is the extension of | | Java is an Object Oriented | +----------------------------+ 3 rows in set (0.00 sec)