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

การใช้ฟังก์ชัน LTRIM() และ RTRIM() ร่วมกับ WHERE clause ใน MySQL

ใช้ LTRIM() และ RTRIM() พร้อมเงื่อนไข WHERE

ฟังก์ชัน LTRIM() และ RTRIM() ของ MySQL สามารถใช้ลบช่องว่างนำหน้าและต่อท้ายจากค่าของคอลัมน์ในตารางได้ และทำงานได้ดีแม้จะมีเงื่อนไขใน WHERE clause

ตัวอย่าง

ตาราง Student มีข้อมูลดังนี้:

1. LTRIM() โดยไม่มี WHERE:

mysql> Select LTRIM(Name) from Student;
+-------------+
| LTRIM(Name) |
+-------------+
| Gaurav      |
| Aarav       |
| Harshit     |
| Gaurav      |
| Yashraj     |
+-------------+
5 rows in set (0.13 sec)

2. RTRIM() โดยไม่มี WHERE:

mysql> Select RTRIM(Name) from Student;
+-------------+
| RTRIM(Name) |
+-------------+
| Gaurav      |
| Aarav       |
| Harshit     |
| Gaurav      |
| Yashraj     |
+-------------+
5 rows in set (0.00 sec)

3. LTRIM() พร้อม WHERE clause:

mysql> Select LTRIM(Name) from Student Where Name = 'Harshit';
+-------------+
| LTRIM(Name) |
+-------------+
| Harshit     |
+-------------+
1 row in set (0.05 sec)

4. RTRIM() พร้อม WHERE clause:

mysql> Select RTRIM(Name) from Student Where Name = 'Gaurav';
+-------------+
| RTRIM(Name) |
+-------------+
| Gaurav      |
| Gaurav      |
+-------------+
2 rows in set (0.00 sec)

ทั้งสองฟังก์ชันทำงานร่วมกับ WHERE clause ได้อย่างถูกต้อง