首页 > 文章列表 > 如何在REPEAT()函数中使用其他的MySQL函数?

如何在REPEAT()函数中使用其他的MySQL函数?

485 2023-09-18

假设我们希望使REPEAT()函数的输出更易读,那么我们可以与其他函数一起使用它。例如,如果我们想在重复的值之间添加空格或其他字符,我们可以使用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*                    |
+-----------------------------------------+
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()函数与其他函数,我们可以使输出更易读。