如何将其他 MySQL 函数与 REPEAT() 函数一起使用?
mysqlmysqli database更新于 2025/5/25 7:08:17
假设我们想让 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() 函数结合使用,我们可以使输出更具可读性。
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

