首頁 > 軟體

MySQL函數CONCAT、CONCAT_WS、GROUP_CONCAT用法詳解

2023-11-02 06:00:32

MySQL函數CONCAT、CONCAT_WS、GROUP_CONCAT

1.concat()函數

CONCAT 函數用於將兩個字串連線為一個字串,試一下下面這個例子:

	SELECT CONCAT('FIRST ', 'SECOND');
    +----------------------------+
    | CONCAT('FIRST ', 'SECOND') |
    +----------------------------+
    | FIRST SECOND               |
    +----------------------------+

要對 CONCAT 函數有更為深入的瞭解,請考慮 employee_tbl 表,表中記錄如下所示:

    SELECT * FROM employee_tbl;
    +------+------+------------+--------------------+
    | id   | name | work_date  | daily_typing_pages |
    +------+------+------------+--------------------+
    |    1 | John | 2022-08-24 |                250 |
    |    2 | Ramr | 2022-08-27 |                220 |
    |    3 | Jack | 2022-08-06 |                170 |
    |    3 | Jack | 2022-08-06 |                100 |
    |    4 | Jill | 2022-08-06 |                220 |
    |    5 | Zara | 2022-08-06 |                300 |
    |    5 | Zara | 2022-08-06 |                350 |
    +------+------+------------+--------------------+

現在,假設你想要將上表中所有的姓名(name)、id和工作日(work_date)連線在一起,那麼可以通過如下的命令來達到目的:

    SELECT CONCAT(id, name, work_date) FROM employee_tbl;
    +-----------------------------+
    | CONCAT(id, name, work_date) |
    +-----------------------------+
    | 1John2022-08-24             |
    | 2Ramr2022-08-27             |
    | 3Jack2022-08-06             |
    | 3Jack2022-08-06             |
    | 4Jill2022-08-06             |
    | 5Zara2022-08-06             |
    | 5Zara2022-08-06             |
    +-----------------------------+

還可以在字元中間新增符號

 SELECT CONCAT(id,",",name,",",work_date) FROM employee_tbl;
    +-----------------------------+
    | CONCAT(id, name, work_date) |
    +-----------------------------+
    | 1,John,2022-08-24           |
    | 2,Ramr,2022-08-27           |
    | 3,Jack,2022-08-06           |
    | 3,Jack,2022-08-06           |
    | 4,Jill,2022-08-06           |
    | 5,Zara,2022-08-06           |
    | 5,Zara,2022-08-06           |
    +-----------------------------+

 SELECT CONCAT(id,",",name,",",work_date) FROM employee_tbl;
    +-----------------------------+
    | CONCAT(id, name, work_date) |
    +-----------------------------+
    | 1,John,2022-08-24           |
    | 2,Ramr,2022-08-27           |
    | 3,Jack,2022-08-06           |
    | 3,Jack,2022-08-06           |
    | 4,Jill,2022-08-06           |
    | 5,Zara,2022-08-06           |
    | 5,Zara,2022-08-06           |
    +-----------------------------+

2.concat_ws()函數

CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的 特殊形式。第一個引數是其它引數的分隔符。分隔符的位置放在要連線的兩個字串之間。分隔符可以是 一個字串,也可以是其它引數。

 SELECT CONCAT(",",id,name,work_date) FROM employee_tbl;
    +-----------------------------+
    | CONCAT(id, name, work_date) |
    +-----------------------------+
    | 1,John,2022-08-24           |
    | 2,Ramr,2022-08-27           |
    | 3,Jack,2022-08-06           |
    | 3,Jack,2022-08-06           |
    | 4,Jill,2022-08-06           |
    | 5,Zara,2022-08-06           |
    | 5,Zara,2022-08-06           |
    +-----------------------------+

3.group_concat()函數

我們可以使用group_concat()函數將一整列列印到一行中,並且可以指定分隔的符號,預設為,

	SELECT GROUP_CONCAT(id) AS ids FROM employee_tbl
    +------------------+
    | GROUP_CONCAT(id) |
    +------------------+
    | 1,2,3,3,4,5,5    |
    +------------------+

將分隔符改為

  	select group_concat(id separator ";") from employee_tbl;
    +------------------+
    | GROUP_CONCAT(id) |
    +------------------+
    | 1;2;3;3;4;5;5    |
    +------------------+

補充:Mysql concat_ws、concat函數使用

一、概念 concat

功能介紹: 將多個字串進行拼接。
語法: concat(欄位,欄位,欄位,…)

concat實踐

select CONCAT(id,',',age,',',name) AS line_info from user

如果連線串中存在NULL,則返回結果為NULL(我沒遇到過):

select CONCAT(id,',',age,',',null) AS line_info from user

Null解決方案:

加一個 IFNULL()函數。

select CONCAT(id,',',age,',',IFNULL(name,'')) AS line_info from user

concat_ws

功能介紹: 字串拼接無需手動新增分隔符(自己理解)。
語法:concat_ws(分隔符,欄位,欄位,…)

concat_ws實踐

select CONCAT_WS(',',id,age,name) AS line_info from user

二、混合使用函數實戰

select CONCAT_WS(',',concat(name),concat('年齡',age,'歲')) AS line_info from user

到此這篇關於MySQL函數CONCAT、CONCAT_WS、GROUP_CONCAT用法詳解的文章就介紹到這了,更多相關MySQL函數CONCAT CONCAT_WS、GROUP_CONCAT內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com