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

คืนค่าสตริงย่อยหลังตัวคั่น (delimiter) ใน MySQL

ใช้ SUBSTRING() เพื่อคืนค่าหลังตัวคั่นครับ

สร้างตารางตัวอย่าง

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

เพิ่มข้อมูลตัวอย่าง

mysql> insert into DemoTable values('John is good in MySQL,Sam is good in MongoDB,Mike is good in Java');
Query OK, 1 row affected (0.19 sec)

แสดงข้อมูลทั้งหมด

mysql> select * from DemoTable;

ผลลัพธ์

+-------------------------------------------------------------------+
| Title                                                             |
+-------------------------------------------------------------------+
| John is good in MySQL,Sam is good in MongoDB,Mike is good in Java |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)

คำสั่งคืนค่าสตริงย่อยหลังตัวคั่น

mysql> select substring(Title, instr(Title, ',') + 1) as AfterDelimiter from DemoTable;

ผลลัพธ์

+---------------------------------------------+
| AfterDelimiter                              |
+---------------------------------------------+
| Sam is good in MongoDB,Mike is good in Java |
+---------------------------------------------+
1 row in set (0.03 sec)

คำอธิบาย: INSTR() หาตำแหน่งของ comma แรก แล้ว SUBSTRING() ตัดเอาตั้งแต่ตำแหน่งถัดไปจนจบสตริง