Mysql 實(shí)現(xiàn)字段拼接的三個(gè)函數(shù)
給運(yùn)營(yíng)導(dǎo)出數(shù)據(jù)時(shí),難免需要對(duì)字段進(jìn)行拼接,如果 Mysql 可以完成的話,就可以少些很多代碼。
- concat()
- concat_ws()
- group_concat()
Mysql 確實(shí)有幾個(gè)函數(shù)可以對(duì)字段進(jìn)行拼接。
concat()
將多個(gè)字段使用空字符串拼接為一個(gè)字段
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)
不過(guò)如果有字段值為 NULL,則結(jié)果為 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()
上面這種方式如果想要使用分隔符分割,就需要每個(gè)字段中間插一個(gè)字符串,非常麻煩。
concat_ws()
可以一次性的解決分隔符的問(wèn)題,并且不會(huì)因?yàn)槟硞€(gè)值為 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()
最后一個(gè)厲害了,正常情況下一個(gè)語(yǔ)句寫(xiě)成這樣一定會(huì)報(bào)錯(cuò)的。
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()
可以將分組狀態(tài)下的其他字段拼接成字符串查詢出來(lái)
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)
默認(rèn)使用逗號(hào)分隔,我們也可以指定分隔符
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)
將字符串按照某個(gè)順序排列
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)
如果想要拼接多個(gè)字段,默認(rèn)是用空字符串進(jìn)行拼接的,我們可以利用 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 實(shí)現(xiàn)字段拼接的三個(gè)函數(shù)的詳細(xì)內(nèi)容,更多關(guān)于MySQL 字符串拼接的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Windows server 2008 r2上安裝MySQL5.7.10步驟
這篇文章主要介紹了Windows server 2008 r2上安裝MySQL5.7.10的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01MySQL自動(dòng)停機(jī)的問(wèn)題處理實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于MySQL自動(dòng)停機(jī)的問(wèn)題處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05安裝MySQL 5.7出現(xiàn)報(bào)錯(cuò):unknown variable ‘mysqlx_port
這篇文章主要介紹了安裝MySQL 5.7出現(xiàn)報(bào)錯(cuò):unknown variable ‘mysqlx_port=0.0‘的解決方法,文中通過(guò)圖文結(jié)合的方式介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06mysql時(shí)間是varchar類型進(jìn)行比較
本文主要介紹了mysql時(shí)間是varchar類型進(jìn)行比較,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04mysql中int(3)和int(10)的數(shù)值范圍是否相同
依稀還記得有次面試,有面試官問(wèn)我int(10)與int(11)有什么區(qū)別,當(dāng)時(shí)覺(jué)得就是長(zhǎng)度的區(qū)別吧,后來(lái)發(fā)現(xiàn)事情不是這么簡(jiǎn)單,這篇文章主要給大家介紹了關(guān)于mysql中int(3)和int(10)的數(shù)值范圍是否相同的相關(guān)資料2021-10-10MySQL使用ReplicationConnection導(dǎo)致連接失效解決
這篇文章主要為大家介紹了MySQL使用ReplicationConnection導(dǎo)致連接失效問(wèn)題分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07