1. 程式人生 > 資料庫 >Mysql 實現欄位拼接的三個函式

Mysql 實現欄位拼接的三個函式

給運營匯出資料時,難免需要對欄位進行拼接,如果 Mysql 可以完成的話,就可以少些很多程式碼。

  • concat()
  • concat_ws()
  • group_concat()

Mysql 確實有幾個函式可以對欄位進行拼接。

concat()

將多個欄位使用空字串拼接為一個欄位

mysql> select concat(id,type) from mm_content limit 10;
+------------------+
| concat(id,type) |
+------------------+
| 100818image   |
| 100824image   |
| 100825video   |
| 100826video   |
| 100827video   |
| 100828video   |
| 100829video   |
| 100830video   |
| 100831video   |
| 100832video   |
+------------------+
10 rows in set (0.00 sec)

不過如果有欄位值為 NULL,則結果為 NULL。

mysql> select concat(id,type,tags) from mm_content limit 10;
+------------------------+
| concat(id,tags) |
+------------------------+
| NULL          |
| NULL          |
| NULL          |
| NULL          |
| NULL          |
| NULL          |
| NULL          |
| NULL          |
| NULL          |
| NULL          |
+------------------------+
10 rows in set (0.00 sec)

concat_ws()

上面這種方式如果想要使用分隔符分割,就需要每個欄位中間插一個字串,非常麻煩。

concat_ws() 可以一次性的解決分隔符的問題,並且不會因為某個值為 NUll,而全部為 NUll。

mysql> select concat_ws(' ',id,tags) from mm_content limit 10;
+--------------------------------+
| concat_ws(' ',tags) |
+--------------------------------+
| 100818 image          |
| 100824 image          |
| 100825 video          |
| 100826 video          |
| 100827 video          |
| 100828 video          |
| 100829 video          |
| 100830 video          |
| 100831 video          |
| 100832 video          |
+--------------------------------+
10 rows in set (0.00 sec)

group_concat()

最後一個厲害了,正常情況下一個語句寫成這樣一定會報錯的。

mysql> select id from test_user group by age;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test_user.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

但是 group_concat() 可以將分組狀態下的其他欄位拼接成字串查詢出來

mysql> select group_concat(name) from test_user group by age;
+--------------------+
| group_concat(name) |
+--------------------+
| wen,ning      |
| wxnacy,win     |
+--------------------+
2 rows in set (0.00 sec)

預設使用逗號分隔,我們也可以指定分隔符

mysql> select group_concat(name separator ' ') from test_user group by age;
+----------------------------------+
| group_concat(name separator ' ') |
+----------------------------------+
| wen ning             |
| wxnacy win            |
+----------------------------------+
2 rows in set (0.00 sec)

將字串按照某個順序排列

mysql> select group_concat(name order by id desc separator ' ') from test_user group by age;
+---------------------------------------------------+
| group_concat(name order by id desc separator ' ') |
+---------------------------------------------------+
| ning wen                     |
| win wxnacy                    |
+---------------------------------------------------+
2 rows in set (0.00 sec)

如果想要拼接多個欄位,預設是用空字串進行拼接的,我們可以利用 concat_ws() 方法巢狀一層

mysql> select group_concat(concat_ws(',',name) separator ' ') from test_user group by age;
+------------------------------------------------------+
| group_concat(concat_ws(',name) separator ' ') |
+------------------------------------------------------+
| 1,wen 2,ning                     |
| 3,wxnacy 4,win                    |
+------------------------------------------------------+
2 rows in set (0.00 sec)

以上就是Mysql 實現欄位拼接的三個函式的詳細內容,更多關於MySQL 字串拼接的資料請關注我們其它相關文章!