MySQL 如何在REPEAT()函数中使用其他MySQL函数?
假设我们想让REPEAT()函数的输出更易读,那么我们可以使用其他函数来协助。例如,如果我们想在重复值之间添加空格或其他字符,则可以使用CONCAT()函数。
阅读更多:MySQL 教程
示例
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* |
+-----------------------------------------+
5 rows in set (0.00 sec)
在下面的示例中,我们同时使用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* ' |
+-----------------------------------------------+
5 rows in set (0.00 sec)
通过将其他函数与REPEAT()函数一起使用,我们可以使输出更易读。