「MySQL」CONCAT_WS関数で区切り文字を指定して文字列を結合する

環境
Windows10 64bit
MySQL 8.0.27

書式
CONCAT_WS( 区切文字列, 文字列1, 文字列2,xxx)
CONCAT_WS関数を使用することで複数の文字列を結合します。
第一引数で区切り文字を指定することができます。

使用例1
引数に区切り文字を文字列で指定します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT CONCAT_WS( 'arkgame', 'study ', ' skill', ' become' ) result;
+-----------------------------------+
| result |
+-----------------------------------+
| study arkgame skillarkgame become |
+-----------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CONCAT_WS( 'arkgame', 'study ', ' skill', ' become' ) result; +-----------------------------------+ | result | +-----------------------------------+ | study arkgame skillarkgame become | +-----------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT CONCAT_WS( 'arkgame', 'study ', ' skill', ' become' ) result;
+-----------------------------------+
| result                            |
+-----------------------------------+
| study arkgame skillarkgame become |
+-----------------------------------+
1 row in set (0.00 sec)

使用例2
引数に文字列を’*’で結合します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT CONCAT_WS( '*', 'study', 'skill', 'become' );
+----------------------------------------------+
| CONCAT_WS( '*', 'study', 'skill', 'become' ) |
+----------------------------------------------+
| study*skill*become |
+----------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CONCAT_WS( '*', 'study', 'skill', 'become' ); +----------------------------------------------+ | CONCAT_WS( '*', 'study', 'skill', 'become' ) | +----------------------------------------------+ | study*skill*become | +----------------------------------------------+ 1 row in set (0.00 sec)
mysql> SELECT CONCAT_WS( '*', 'study', 'skill', 'become' );
+----------------------------------------------+
| CONCAT_WS( '*', 'study', 'skill', 'become' ) |
+----------------------------------------------+
| study*skill*become                           |
+----------------------------------------------+
1 row in set (0.00 sec)

使用例3
引数にnullが含まれている場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT CONCAT_WS( '(', 'study', NULL, 'skill' ) result;
+-------------+
| result |
+-------------+
| study(skill |
+-------------+
1 row in set (0.00 sec)
mysql> SELECT CONCAT_WS( '(', 'study', NULL, 'skill' ) result; +-------------+ | result | +-------------+ | study(skill | +-------------+ 1 row in set (0.00 sec)
mysql> SELECT CONCAT_WS( '(', 'study', NULL, 'skill' ) result;
+-------------+
| result      |
+-------------+
| study(skill |
+-------------+
1 row in set (0.00 sec)

使用例4
引数に数値を含む場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT CONCAT_WS( '+', 55, 66, -77 ) result;
+-----------+
|  result |
+-----------+
| 55+66+-77 |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT CONCAT_WS( '+', 55, 66, -77 ) result; +-----------+ |  result | +-----------+ | 55+66+-77 | +-----------+ 1 row in set (0.00 sec)
mysql> SELECT CONCAT_WS( '+', 55, 66, -77 ) result;
+-----------+
|  result  |
+-----------+
| 55+66+-77 |
+-----------+
1 row in set (0.00 sec)

 

MySQL

Posted by arkgame