如何使用其他 MySQL 函式與 REPEAT() 函式?


假如我們想要使 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() 函式一起使用,我們可以使輸出更具可讀性。

更新於: 2020 年 2 月 7 日

81 次瀏覽

開啟你的 職業

透過完成本課程獲得認證

開始
廣告
© . All rights reserved.