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

วิธีใช้ฟังก์ชัน MySQL อื่นๆ ร่วมกับ REPEAT()

รวมฟังก์ชันอื่นกับ REPEAT() เพื่อผลลัพธ์ที่อ่านง่ายขึ้น

หากต้องการให้ผลลัพธ์ของ REPEAT() อ่านง่ายขึ้น สามารถใช้ฟังก์ชันอื่นร่วมกันได้ เช่น ใช้ CONCAT() เพื่อเพิ่มช่องว่างหรืออักขระคั่นระหว่างค่าที่ทำซ้ำ

ตัวอย่าง 1: ใช้ CONCAT() เพิ่มคั่นระหว่างค่าซ้ำ

mysql> Select REPEAT(CONCAT(' *',Subject,'* '),3) AS Subject_repetition from student;
+-----------------------------------------+
| Subject_repetition                      |
+-----------------------------------------+
| *Computers* *Computers* *Computers*     |
| *History* *History* *History*           |
| *Commerce* *Commerce* *Commerce*        |
| *Computers* *Computers* *Computers*     |
| *Math* *Math* *Math*                    |
+-----------------------------------------+

ตัวอย่าง 2: ใช้ QUOTE() และ CONCAT() ร่วมกับ REPEAT():

mysql> Select REPEAT(QUOTE(CONCAT(' *',Subject,'* ')),3) AS Subject_repetition from student;
+-----------------------------------------------+
| Subject_repetition                            |
+-----------------------------------------------+
| ' *Computers* '' *Computers* '' *Computers* ' |
| ' *History* '' *History* '' *History* '       |
| ' *Commerce* '' *Commerce* '' *Commerce* '    |
| ' *Computers* '' *Computers* '' *Computers* ' |
| ' *Math* '' *Math* '' *Math* '                |
+-----------------------------------------------+

การรวมฟังก์ชันช่วยจัดรูปแบบผลลัพธ์ให้อ่านง่ายและชัดเจนขึ้น