MySQL - CONCAT


UDEMY-The Ultimate MySQL Bootca…The Ultimate MySQL Bootcamp:Go from SQL Beginner to Expert
入る前に、MySQL公式サイトに入るとマニュアルがあります.

公式サイトに直接アクセスして、DOCUMENTATIONに入って資料を探すことができますが、Googleで検索するときは、MYSQL STRING FUNCTIONのような方法で検索すると便利です.

CONCAT、MySQL String Functionの1つ


CONCAT : Combine Data For Cleaner Output


次のデータがあります.
mysql> select * from books;
+---------+-----------------------------------------------------+--------------+----------------+---------------+----------------+-------+
| book_id | title                                               | author_fname | author_lname   | released_year | stock_quantity | pages |
+---------+-----------------------------------------------------+--------------+----------------+---------------+----------------+-------+
|       1 | The Namesake                                        | Jhumpa       | Lahiri         |          2003 |             32 |   291 |
|       2 | Norse Mythology                                     | Neil         | Gaiman         |          2016 |             43 |   304 |
|       3 | American Gods                                       | Neil         | Gaiman         |          2001 |             12 |   465 |
|       4 | Interpreter of Maladies                             | Jhumpa       | Lahiri         |          1996 |             97 |   198 |
|       5 | A Hologram for the King: A Novel                    | Dave         | Eggers         |          2012 |            154 |   352 |
|       6 | The Circle                                          | Dave         | Eggers         |          2013 |             26 |   504 |
|       7 | The Amazing Adventures of Kavalier & Clay           | Michael      | Chabon         |          2000 |             68 |   634 |
|       8 | Just Kids                                           | Patti        | Smith          |          2010 |             55 |   304 |
|       9 | A Heartbreaking Work of Staggering Genius           | Dave         | Eggers         |          2001 |            104 |   437 |
|      10 | Coraline                                            | Neil         | Gaiman         |          2003 |            100 |   208 |
|      11 | What We Talk About When We Talk About Love: Stories | Raymond      | Carver         |          1981 |             23 |   176 |
|      12 | Where I'm Calling From: Selected Stories            | Raymond      | Carver         |          1989 |             12 |   526 |
|      13 | White Noise                                         | Don          | DeLillo        |          1985 |             49 |   320 |
|      14 | Cannery Row                                         | John         | Steinbeck      |          1945 |             95 |   181 |
|      15 | Oblivion: Stories                                   | David        | Foster Wallace |          2004 |            172 |   329 |
|      16 | Consider the Lobster                                | David        | Foster Wallace |          2005 |             92 |   343 |
+---------+-----------------------------------------------------+--------------+----------------+---------------+----------------+-------+
16 rows in set (0.00 sec)
ここにauthor fname(firstname)コラムとauthor lname(lastname)コラムがあります.ここで、フルネームでデータを取得したい場合は、CONCATを使用します.CONCAT (columnName, anotherColunName) . 真ん中に他の文字を加えることもできます.CONCAT (columnName, 'some text', anotherColunName, 'more text') .
だからfirstnameとlastnameの間にスペースを入れたいなら、CONCAT (author_fname,' ',author_lanme)という方法で書くことができます.
まずCONCATを試してみましょう.
mysql> CONCAT('Hello','World');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONCAT('Hello','World')' at line 1
CONCATを使用する場合、CONCATを単独で使用することはできません.選択肢がないからです.SELECTと一緒に使いましょう.
mysql> SELECT CONCAT ('Hello', '~','World','!');
+-----------------------------------+
| CONCAT ('Hello', '~','World','!') |
+-----------------------------------+
| Hello~World!                      |
+-----------------------------------+
1 row in set (0.00 sec)
次に、テーブルからデータを選択して操作します.
mysql> SELECT
    -> CONCAT(author_fname,' ',author_lname)
    -> FROM books;
+---------------------------------------+
| CONCAT(author_fname,' ',author_lname) |
+---------------------------------------+
| Jhumpa Lahiri                         |
| Neil Gaiman                           |
| Neil Gaiman                           |
| Jhumpa Lahiri                         |
| Dave Eggers                           |
| Dave Eggers                           |
| Michael Chabon                        |
| Patti Smith                           |
| Dave Eggers                           |
| Neil Gaiman                           |
| Raymond Carver                        |
| Raymond Carver                        |
| Don DeLillo                           |
| John Steinbeck                        |
| David Foster Wallace                  |
| David Foster Wallace                  |
+---------------------------------------+
16 rows in set (0.01 sec)
CONCAT(author_fname,' ',author_lname)で表示されるコラム名が気に入らない場合は、ASを使用してコラム名を異なる方法で出力できます.
mysql> SELECT
    -> CONCAT (author_fname,' ',author_lname) AS 'Full Name'
    -> FROM books;
+----------------------+
| Full Name            |
+----------------------+
| Jhumpa Lahiri        |
| Neil Gaiman          |
| Neil Gaiman          |
| Jhumpa Lahiri        |
| Dave Eggers          |
| Dave Eggers          |
| Michael Chabon       |
| Patti Smith          |
| Dave Eggers          |
| Neil Gaiman          |
| Raymond Carver       |
| Raymond Carver       |
| Don DeLillo          |
| John Steinbeck       |
| David Foster Wallace |
| David Foster Wallace |
+----------------------+
16 rows in set (0.00 sec)

CONCAT_WS : Concat With Separator

mysql> SELECT
    -> CONCAT_WS(' - ', title, author_fname, author_lname)
    -> from books;
+------------------------------------------------------------------------+
| CONCAT_WS(' - ', title, author_fname, author_lname)                    |
+------------------------------------------------------------------------+
| The Namesake - Jhumpa - Lahiri                                         |
| Norse Mythology - Neil - Gaiman                                        |
| American Gods - Neil - Gaiman                                          |
| Interpreter of Maladies - Jhumpa - Lahiri                              |
| A Hologram for the King: A Novel - Dave - Eggers                       |
| The Circle - Dave - Eggers                                             |
| The Amazing Adventures of Kavalier & Clay - Michael - Chabon           |
| Just Kids - Patti - Smith                                              |
| A Heartbreaking Work of Staggering Genius - Dave - Eggers              |
| Coraline - Neil - Gaiman                                               |
| What We Talk About When We Talk About Love: Stories - Raymond - Carver |
| Where I'm Calling From: Selected Stories - Raymond - Carver            |
| White Noise - Don - DeLillo                                            |
| Cannery Row - John - Steinbeck                                         |
| Oblivion: Stories - David - Foster Wallace                             |
| Consider the Lobster - David - Foster Wallace                          |
+------------------------------------------------------------------------+
16 rows in set (0.00 sec)
JavascriptのArray prototypeのjoin()関数と似ています.(join(' - ')がこのように行われる場合、join()は、CONCAT()と同様にパラメータを与えない.)