ใช้ SUBSTRING_INDEX() ดึงค่าตำแหน่งที่ n จากสตริงคั่นคอมมา
สร้างตารางและเพิ่มข้อมูล
mysql> create table DemoTable1615 ( ListOfSubject text ); Query OK, 0 rows affected (0.81 sec)
mysql> insert into DemoTable1615 values('Python,Java,MySQL,MongoDB,C,C++,ASP.net');
Query OK, 1 row affected (0.19 sec)
คิวรี่ดึงค่าตำแหน่งที่ 4
mysql> select substring_index(substring_index(ListOfSubject, ',', 4), ',', -1) as Position from DemoTable1615;
ผลลัพธ์
+----------+ | Position | +----------+ | MongoDB | +----------+ 1 row in set (0.00 sec)
หลักการ: SUBSTRING_INDEX(str, delim, count) | substring_index(ListOfSubject, ',', 4) ดึง 4 ค่าแรก: 'Python,Java,MySQL,MongoDB' | substring_index(..., ',', -1) ดึงส่วนหลังคอมมาสุดท้าย: 'MongoDB' | count บวก = นับจากซ้าย, ลบ = นับจากขวา