mysql中GROUP_CONCAT函數(shù)使用技巧及問題詳解
概要
` group_concat函數(shù)是mysql中非常實(shí)用的函數(shù),它可以將同一個(gè)分組下的行拼接在一起。其完整語法:
GROUP_CONCAT([DISTINCT] 要連接的字段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])
使用技巧
1. 建表、插入數(shù)據(jù)
#建表語句 CREATE TABLE `test_group_concat` ( `id` int(11) NOT NULL COMMENT 'id', `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; #插入測(cè)試數(shù)據(jù) INSERT INTO `test`.`test_group_concat` (`id`, `age`) VALUES (1, 20); INSERT INTO `test`.`test_group_concat` (`id`, `age`) VALUES (1, 20); INSERT INTO `test`.`test_group_concat` (`id`, `age`) VALUES (1, 10); INSERT INTO `test`.`test_group_concat` (`id`, `age`) VALUES (3, 30); INSERT INTO `test`.`test_group_concat` (`id`, `age`) VALUES (3, 40); INSERT INTO `test`.`test_group_concat` (`id`, `age`) VALUES (3, 40); INSERT INTO `test`.`test_group_concat` (`id`, `age`) VALUES (4, 50); INSERT INTO `test`.`test_group_concat` (`id`, `age`) VALUES (4, 60); #基本查詢 SELECT * FROM test_group_concat
2.以id分組,把a(bǔ)ge字段的值拼成一行,逗號(hào)分隔(默認(rèn))
select id,group_concat(age) from test_group_concat group by id;
3.以id分組,把a(bǔ)ge字段的值拼成 一行,分號(hào)分隔
select id,group_concat(age separator ';') from test_group_concat group by id;
4.以id分組,把去冗余的age字段的值打印在一行
select id,group_concat(distinct age) from test_group_concat group by id;
5.以id分組,把a(bǔ)ge字段的值打印在一行,逗號(hào)分隔,以age排倒序
select id,group_concat(age order by age desc) from test_group_concat group by id;
發(fā)現(xiàn)問題
在項(xiàng)目用到如下sql
SELECT GROUP_CONCAT( c.goods_detail_id ) FROM air_out_order a LEFT JOIN air_out_order_detail b ON b.order_id = a.id LEFT JOIN air_out_good_record c ON c.order_detail_id = b.id;
查詢結(jié)果:
713278402284617792,722329309878140931,722329309878140949,722329309878140973,722329309878140939,715342356376936480,715342356376936482,715342356381130806,715342356376936484,715342356381130810,713278240841662516,713278240841662476,713278240841662520,715342356381130876,715342356381130940,715342356385325066,715342356385325132,715342356385325196,715342356385325260,715342356376936526,715342356381130812,715342356381130890,715342356381130964,744788201669005316,744788201669005380,744788201673199622,744788201669005330,744788201669005394,744788201752891404,744788201664811012,744788201669005344,744788201669005408,744788201752891418,744788201669005338,744788201669005410,744788201664811016,744788201669005354,744788201669005424,744788201664811030,744788201669005368,744788201673199616,744788201752891400,744788201669005312,745061736895352916,745061736891158544,745061736895352852,745061736895352930,745061736891158558,745061736895352866,745061736895352944,745061736891158572,745061736895352880,745061736895352958,74506173689115858
而實(shí)際上
SELECT c.goods_detail_id FROM air_out_order a LEFT JOIN air_out_order_detail b ON b.order_id = a.id LEFT JOIN air_out_good_record c ON c.order_detail_id = b.id;
的查詢結(jié)果為:
713278402284617792
722329309878140931,722329309878140949,722329309878140973,722329309878140939
715342356376936480
715342356376936482
715342356381130806
715342356376936484
715342356381130810
713278240841662516
713278240841662476
713278240841662520
715342356381130876,715342356381130940,715342356385325066,715342356385325132,715342356385325196,715342356385325260,715342356376936526,715342356381130812,715342356381130890,715342356381130964
744788201669005316,744788201669005380,744788201673199622,744788201669005330,744788201669005394,744788201752891404,744788201664811012,744788201669005344,744788201669005408,744788201752891418
744788201669005338,744788201669005410,744788201664811016,744788201669005354,744788201669005424,744788201664811030,744788201669005368,744788201673199616,744788201752891400,744788201669005312
745061736895352916,745061736891158544,745061736895352852,745061736895352930,745061736891158558,745061736895352866,745061736895352944,745061736891158572,745061736895352880,745061736895352958,745061736891158586,745061736895352894,745061736891158536,745061736895352844,745061736895352908,745061736895352922,745061736891158550,745061736895352858,745061736895352936,745061736891158564,745061736895352872,745061736895352950,745061736891158578,745061736895352886,745061736895352964,745061736891158528,745061736895352836,745061736895352900,745061736895352914,745061736891158542
744788201669005318,744788201669005396,744788201669005334,744788201669005414,744788201669005352,744788201669005430,744788201664811022,744788201669005370,744788201752891394,744788201664811038
744788201664811020,744788201669005382,744788201752891416,744788201664811040,744788201669005400,744788201669005328,744788201669005420,744788201669005350,744788201673199618,744788201664811008
746171271546650637
746171271550844964
745061736895352842,745061736895352934,745061736895352862,745061736895352954,745061736891158548,745061736895352882,745061736891158568,745061736895352902,745061736895352832,745061736895352924
747357984740646912,747357984912613376,747357984782589952,747357984824532992,747357984652566528
747357984778395648
747357984753229824
745898989121159210,745898989108576268,745898989125353485,745898989116964869,745898989121159170,745898989121159184
745898989112770564,745898989121159168,745898989121159186,745898989121159202,745898989108576272,745898989125353481
745898989108576274,745898989116964877,745898989121159182,745898989121159206,745898989108576258,745898989116964865
745898989116964879
745898989112770562
745898989116964871,745898989121159180,745898989121159208,745898989108576260,745898989121159172,745898989121159194
745898989121159188,745898989125353477,745898989116964875,745898989121159198,745898989108576264,745898989121159178
747357984694509568,747357984904224768,747357984749035520,747357984807755776,747357984849698816,747357984644177920,747357984883253248,747357984715481088,747357984770007040,747357984828727296
747357984707092480,747357984795172864,747357984853893120,747357984895836160,747357984665149440,747357984736452608,747357984820338688,747357984866476032,747357984509960192,747357984916807680
745898989121159196
745898989121159200
762832940033040386
715342356385325082
要多得多,出現(xiàn)group_concat把數(shù)據(jù)給截?cái)嗔?,什么原因呢?/p>
解決問題
由于group_concat有長(zhǎng)度限制,默認(rèn)1024個(gè)字符,
所以解決的辦法也就是將group_concat有長(zhǎng)度限制設(shè)置足夠大即可。
1)臨時(shí)解決方案:
在命令行運(yùn)行
SET SESSION group_concat_max_len = 18446744073709551615; set global max_allowed_packet = 2*1024*1024*10;
2)永久解決方案:
首先找到mysql的配置文件,設(shè)置如下配置
max_allowed_packet = 20M group_concat_max_len = 18446744073709551615
總結(jié)
到此這篇關(guān)于mysql中GROUP_CONCAT函數(shù)使用技巧及問題詳解的文章就介紹到這了,更多相關(guān)mysql GROUP_CONCAT函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MySQL統(tǒng)計(jì)函數(shù)GROUP_CONCAT使用陷阱分析
- mysql group_concat()函數(shù)用法總結(jié)
- 淺談MySQL中g(shù)roup_concat()函數(shù)的排序方法
- MySQL拼接字符串函數(shù)GROUP_CONCAT詳解
- mysql中GROUP_CONCAT的使用方法實(shí)例分析
- MySQL基于group_concat()函數(shù)合并多行數(shù)據(jù)
- MySQL中CONCAT和GROUP_CONCAT方法的區(qū)別詳解
- MySQL函數(shù)CONCAT、CONCAT_WS、GROUP_CONCAT用法詳解
- MySQL group_concat函數(shù)使用方法詳解
- mysql中GROUP_CONCAT函數(shù)使用及遇到問題詳解
- MySQL中g(shù)roup_concat函數(shù)用法小結(jié)
相關(guān)文章
關(guān)于mysql delete的問題小結(jié)
關(guān)于mysql delete的問題,需要的朋友可以參考下。2011-05-05mysql 5.0.67最新版替代MySQL 5.0.51b版本官方下載
發(fā)布說明MySQL服務(wù)器5.0.67 ( 2008年8月4日) 這是一個(gè)bugfix釋放現(xiàn)有生產(chǎn)釋放的家庭。它取代MySQL的5.0.51b 。2008-08-08Mac安裝 mysql 數(shù)據(jù)庫總結(jié)
本文給大家分享的是如何在Mac下安裝mysql數(shù)據(jù)庫的方法,總結(jié)的很全面,有需要的小伙伴可以參考下2016-04-04關(guān)于mysql主備切換canal出現(xiàn)的問題解決
這篇文章主要給大家介紹了關(guān)于mysql主備切換canal出現(xiàn)的一些問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11windows下安裝、卸載mysql服務(wù)的方法(mysql 5.6 zip解壓版安裝教程)
這篇文章主要介紹了windows下安裝、卸載mysql服務(wù)的方法(zip解壓版安裝),需要的朋友可以參考下2016-06-06Mysql 5.7.18安裝方法及啟動(dòng)MySQL服務(wù)的過程詳解
這篇文章主要介紹了Mysql 5.7.18安裝方法及啟動(dòng)MySQL服務(wù)的過程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05