好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 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, type, 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, type, tags) from mm_content limit 10;
+--------------------------------+
| concat_ws(' ', id, type, 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(',', id, name) separator ' ') from test_user group by age;
+------------------------------------------------------+
| group_concat(concat_ws(',', id, name) separator ' ') |
+------------------------------------------------------+
| 1,wen 2,ning                     |
| 3,wxnacy 4,win                    |
+------------------------------------------------------+
2 rows in set (0.00 sec)

以上就是Mysql 實現字段拼接的三個函數的詳細內容,更多關于MySQL 字符串拼接的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • Mysql合并結果接橫向拼接字段的實現步驟
  • MySQL拼接字符串函數GROUP_CONCAT詳解
  • mysql 多個字段拼接的實例詳解

標簽:天津 阿里 合肥 牡丹江 呼和浩特 惠州 公主嶺 沈陽

巨人網絡通訊聲明:本文標題《Mysql 實現字段拼接的三個函數》,本文關鍵詞  Mysql,實現,字段,拼接,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Mysql 實現字段拼接的三個函數》相關的同類信息!
  • 本頁收集關于Mysql 實現字段拼接的三個函數的相關信息資訊供網民參考!
  • 推薦文章