สำหรับเรื่องนี้ ใช้ฟังก์ชัน REPLACE() แทนที่ช่องว่างด้วยอักขระที่ต้องการครับ
สร้างตารางตัวอย่าง
mysql> create table DemoTable (Subject text); Query OK, 0 rows affected (0.86 sec)
เพิ่มข้อมูลตัวอย่าง
mysql> insert into DemoTable values('Introduction to MySQL');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Java in depth');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('Data Structure and Algorithm');
Query OK, 1 row affected (0.16 sec)
แสดงข้อมูลทั้งหมด
mysql> select * from DemoTable;
ผลลัพธ์
+------------------------------+ | Subject | +------------------------------+ | Introduction to MySQL | | Java in depth | | Data Structure and Algorithm | +------------------------------+ 3 rows in set (0.00 sec)
คำสั่งแทนที่ช่องว่าง
mysql> SELECT *, REPLACE(Subject, ' ', '_') as Subject_Name from DemoTable;
ผลลัพธ์
+-------------------------------+------------------------------+ | Subject | Subject_Name | +-------------------------------+------------------------------+ | Introduction to MySQL | Introduction_to_MySQL | | Java in depth | Java_in_depth | | Data Structure and Algorithm | Data_Structure_and_Algorithm | +-------------------------------+------------------------------+ 3 rows in set (0.00 sec)
คำอธิบาย: REPLACE(คอลัมน์, 'ค่าต้นฉบับ', 'ค่าใหม่') แทนที่ช่องว่าง ' ' ด้วย '_' แสดงผลในคอลัมน์ใหม่ Subject_Name