亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Mysql 實現字段拼接的三個函數

瀏覽:122日期:2023-10-09 10:55:10

給運營導出數據時,難免需要對字段進行拼接,如果 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 數據庫
相關文章:
主站蜘蛛池模板: 一级色黄 | 经典香港a毛片免费观看 | 黄色片在线看 | 中国china体内裑精亚洲毛片 | 中国国产一国产一级毛片视频 | 91精品手机国产免费 | www黄色| 免费看精品黄线在线观看 | 亚洲视频手机在线 | 欧美日韩在线看 | 香蕉视频网站在线观看 | 日韩中文在线视频 | 欧美在线成人午夜网站 | 日韩综合图区 | 美国大片成人性网 | 亚洲色图88 | 免费又爽又黄禁片观看1000 | 在线看av的网址 | 国产激情一区二区三区在线观看 | 免费簧网站永久在线播放国产 | 国产午夜大片 | 中日韩毛片 | 黄色一级片免费看 | 网址在线 | 久久国产香蕉 | 青青操在线观看 | 美国大黄一片免费看 | 亚洲成年人网 | 国产精品一区二区手机看片 | 日韩高清专区 | 国产综合第一页在线视频 | 日韩 亚洲 欧美 中文 高清 | 国产3区 | 色综合久久久久久久久五月性色 | 手机在线观看视频你懂的 | 国产成人精品一区二三区在线观看 | 欧美a级片视频 | 亚洲欧美日韩一级特黄在线 | 在线欧美视频免费观看国产 | 国产亚洲精品视频中文字幕 | 亚洲第一页在线播放 |