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

จำกัดความยาว LONGTEXT ในผลลัพธ์ MySQL ด้วย SUBSTRING()

ใช้ SUBSTRING(str, start, length) ตัดข้อความยาวใน SELECT รูปแบบ:

SELECT SUBSTRING(yourColumnName, 1, maxLength) AS shortText FROM yourTableName;

ตัวอย่าง

mysql> CREATE TABLE limitLengthOfLongTextDemo (sentence LONGTEXT);
mysql> INSERT INTO limitLengthOfLongTextDemo VALUES
    -> ('This is the introduction to MySQL'),
    -> ('PL/SQL is the extension of Structured Query Language'),
    -> ('Java is an Object Oriented Programming Language');
mysql> SELECT SUBSTRING(sentence, 1, 26) AS `26Characters` FROM limitLengthOfLongTextDemo;

ผลลัพธ์:

+----------------------------+
| 26Characters               |
+----------------------------+
| This is the introduction t |
| PL/SQL is the extension of |
| Java is an Object Oriented |
+----------------------------+

ประโยชน์: แสดง Preview ข้อความยาวในหน้ารายการ (List View) โดยไม่ต้องดึงข้อมูลเต็มลด Bandwidth และ Memory