การตัดข้อความแสดงเฉพาะ 200 ตัวอักษรแรกใน MySQL
ใช้ฟังก์ชัน LEFT() เพื่อดึงตัวอักษรจากด้านซ้าย (จุดเริ่มต้น) ของสตริงตามจำนวนที่กำหนด
SELECT LEFT(yourColumnName, 200) AS alias_name FROM yourTableName;
ตัวอย่างการใช้งาน
สร้างตารางและเพิ่มข้อความยาว:
mysql> create table DemoTable (Paragraph longtext);
mysql> insert into DemoTable values('Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Server,Introduction to ASP.net,Introduction to JSF');
ข้อมูลต้นฉบับ:
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Paragraph | +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Server,Introduction to ASP.net,Introduction to JSF | +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
คิวรีแสดง 200 ตัวอักษรแรก:
mysql> select left(Paragraph, 200) AS `200Characters` from DemoTable;
ผลลัพธ์:
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 200Characters | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Introduction to Java,Introduction to C,Introduction to C++,Introduction to Spring,Introduction to Hibernate,Introduction to Python,Introduction to MySQL,Introduction to MongoDB,Introduction to SQL Ser | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
ฟังก์ชันที่เกี่ยวข้อง:
RIGHT(str, len)- ดึงตัวอักษรจากด้านขวา (ท้ายสตริง)SUBSTRING(str, pos, len)- ดึงส่วนของสตริงจากตำแหน่งที่กำหนดSUBSTR()- ตัวย่อของ SUBSTRING