📄 func_gconcat.result
字号:
drop table if exists t1, t2;create table t1 (grp int, a bigint unsigned, c char(10) not null, d char(10) not null);insert into t1 values (1,1,"a","a");insert into t1 values (2,2,"b","a");insert into t1 values (2,3,"c","b");insert into t1 values (3,4,"E","a");insert into t1 values (3,5,"C","b");insert into t1 values (3,6,"D","b");insert into t1 values (3,7,"d","d");insert into t1 values (3,8,"d","d");insert into t1 values (3,9,"D","c");select grp,group_concat(c) from t1 group by grp;grp group_concat(c)1 a2 b,c3 E,C,D,d,d,Dexplain extended select grp,group_concat(c) from t1 group by grp;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ALL NULL NULL NULL NULL 9 Using filesortWarnings:Note 1003 select `test`.`t1`.`grp` AS `grp`,group_concat(`test`.`t1`.`c` separator ',') AS `group_concat(c)` from `test`.`t1` group by `test`.`t1`.`grp`select grp,group_concat(a,c) from t1 group by grp;grp group_concat(a,c)1 1a2 2b,3c3 4E,5C,6D,7d,8d,9Dselect grp,group_concat("(",a,":",c,")") from t1 group by grp;grp group_concat("(",a,":",c,")")1 (1:a)2 (2:b),(3:c)3 (4:E),(5:C),(6:D),(7:d),(8:d),(9:D)select grp,group_concat(c separator ",") from t1 group by grp;grp group_concat(c separator ",")1 a2 b,c3 E,C,D,d,d,Dselect grp,group_concat(c separator "---->") from t1 group by grp;grp group_concat(c separator "---->")1 a2 b---->c3 E---->C---->D---->d---->d---->Dselect grp,group_concat(c order by c) from t1 group by grp;grp group_concat(c order by c)1 a2 b,c3 C,D,d,d,D,Eselect grp,group_concat(c order by c desc) from t1 group by grp;grp group_concat(c order by c desc)1 a2 c,b3 E,D,d,d,D,Cselect grp,group_concat(d order by a) from t1 group by grp;grp group_concat(d order by a)1 a2 a,b3 a,b,b,d,d,cselect grp,group_concat(d order by a desc) from t1 group by grp;grp group_concat(d order by a desc)1 a2 b,a3 c,d,d,b,b,aselect grp,group_concat(a order by a,d+c-ascii(c)-a) from t1 group by grp;grp group_concat(a order by a,d+c-ascii(c)-a)1 12 2,33 4,5,6,7,8,9select grp,group_concat(a order by d+c-ascii(c),a) from t1 group by grp;grp group_concat(a order by d+c-ascii(c),a)1 12 3,23 7,8,4,6,9,5select grp,group_concat(c order by 1) from t1 group by grp;grp group_concat(c order by 1)1 a2 b,c3 C,D,d,d,D,Eselect grp,group_concat(c order by "c") from t1 group by grp;grp group_concat(c order by "c")1 a2 b,c3 C,D,d,d,D,Eselect grp,group_concat(distinct c order by c) from t1 group by grp;grp group_concat(distinct c order by c)1 a2 b,c3 C,D,Eselect grp,group_concat(distinct c order by c desc) from t1 group by grp;grp group_concat(distinct c order by c desc)1 a2 c,b3 E,D,Cexplain extended select grp,group_concat(distinct c order by c desc) from t1 group by grp;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ALL NULL NULL NULL NULL 9 Using filesortWarnings:Note 1003 select `test`.`t1`.`grp` AS `grp`,group_concat(distinct `test`.`t1`.`c` order by `test`.`t1`.`c` DESC separator ',') AS `group_concat(distinct c order by c desc)` from `test`.`t1` group by `test`.`t1`.`grp`select grp,group_concat(c order by c separator ",") from t1 group by grp;grp group_concat(c order by c separator ",")1 a2 b,c3 C,D,d,d,D,Eselect grp,group_concat(c order by c desc separator ",") from t1 group by grp;grp group_concat(c order by c desc separator ",")1 a2 c,b3 E,D,d,d,D,Cselect grp,group_concat(distinct c order by c separator ",") from t1 group by grp;grp group_concat(distinct c order by c separator ",")1 a2 b,c3 C,D,Eexplain extended select grp,group_concat(distinct c order by c separator ",") from t1 group by grp;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ALL NULL NULL NULL NULL 9 Using filesortWarnings:Note 1003 select `test`.`t1`.`grp` AS `grp`,group_concat(distinct `test`.`t1`.`c` order by `test`.`t1`.`c` ASC separator ',') AS `group_concat(distinct c order by c separator ",")` from `test`.`t1` group by `test`.`t1`.`grp`select grp,group_concat(distinct c order by c desc separator ",") from t1 group by grp;grp group_concat(distinct c order by c desc separator ",")1 a2 c,b3 E,D,Cselect grp,group_concat(c order by grp desc) from t1 group by grp order by grp;grp group_concat(c order by grp desc)1 a2 c,b3 D,d,d,D,C,Eselect grp, group_concat(a separator "")+0 from t1 group by grp;grp group_concat(a separator "")+01 12 233 456789select grp, group_concat(a separator "")+0.0 from t1 group by grp;grp group_concat(a separator "")+0.01 1.02 23.03 456789.0select grp, ROUND(group_concat(a separator "")) from t1 group by grp;grp ROUND(group_concat(a separator ""))1 12 233 456789drop table t1;create table t1 (grp int, c char(10));insert into t1 values (1,NULL),(2,"b"),(2,NULL),(3,"E"),(3,NULL),(3,"D"),(3,NULL),(3,NULL),(3,"D"),(4,""),(5,NULL);select grp,group_concat(c order by c) from t1 group by grp;grp group_concat(c order by c)1 NULL2 b3 D,D,E4 5 NULLset group_concat_max_len = 4;select grp,group_concat(c) from t1 group by grp;grp group_concat(c)1 NULL2 b3 D,D,4 5 NULLWarnings:Warning 1260 1 line(s) were cut by GROUP_CONCAT()show warnings;Level Code MessageWarning 1260 1 line(s) were cut by GROUP_CONCAT()set group_concat_max_len = 1024;select group_concat(sum(c)) from t1 group by grp;ERROR HY000: Invalid use of group functionselect grp,group_concat(c order by 2) from t1 group by grp;ERROR 42S22: Unknown column '2' in 'order clause'drop table t1;create table t1 ( URL_ID int(11), URL varchar(80));create table t2 ( REQ_ID int(11), URL_ID int(11));insert into t1 values (4,'www.host.com'), (5,'www.google.com'),(5,'www.help.com');insert into t2 values (1,4), (5,4), (5,5);select REQ_ID, Group_Concat(URL) as URL from t1, t2 wheret2.URL_ID = t1.URL_ID group by REQ_ID;REQ_ID URL1 X5 X,X,Xselect REQ_ID, Group_Concat(URL) as URL, Min(t1.URL_ID) urll,Max(t1.URL_ID) urlg from t1, t2 where t2.URL_ID = t1.URL_ID group by REQ_ID;REQ_ID URL urll urlg1 X 4 45 X,X,X 4 5drop table t1;drop table t2;create table t1 (id int, name varchar(16));insert into t1 values (1,'longername'),(1,'evenlongername');select ifnull(group_concat(concat(t1.id, ':', t1.name)), 'shortname') as 'without distinct: how it should be' from t1;without distinct: how it should be1:longername,1:evenlongernameselect distinct ifnull(group_concat(concat(t1.id, ':', t1.name)), 'shortname') as 'with distinct: cutoff at length of shortname' from t1;with distinct: cutoff at length of shortname1:longername,1:evenlongernamedrop table t1;create table t1(id int);create table t2(id int);insert into t1 values(0),(1);select group_concat(t1.id) FROM t1,t2;group_concat(t1.id)NULLdrop table t1;drop table t2;create table t1 (bar varchar(32));insert into t1 values('test1'),('test2');select group_concat(bar order by concat(bar,bar)) from t1;group_concat(bar order by concat(bar,bar))test1,test2select group_concat(bar order by concat(bar,bar) desc) from t1;group_concat(bar order by concat(bar,bar) desc)test2,test1select bar from t1 having group_concat(bar)='';barselect bar from t1 having instr(group_concat(bar), "test") > 0;bartest1select bar from t1 having instr(group_concat(bar order by concat(bar,bar) desc), "test2,test1") > 0;bartest1drop table t1;create table t1 (a int, a1 varchar(10));create table t2 (a0 int);insert into t1 values (0,"a"),(0,"b"),(1,"c");insert into t2 values (1),(2),(3);select group_concat(a1 order by (t1.a IN (select a0 from t2))) from t1;group_concat(a1 order by (t1.a IN (select a0 from t2)))b,a,cselect group_concat(a1 order by (t1.a)) from t1;group_concat(a1 order by (t1.a))b,a,cdrop table t1, t2;CREATE TABLE t1 (id1 tinyint(4) NOT NULL, id2 tinyint(4) NOT NULL);INSERT INTO t1 VALUES (1, 1),(1, 2),(1, 3),(1, 4),(1, 5),(2, 1),(2, 2),(2, 3);CREATE TABLE t2 (id1 tinyint(4) NOT NULL);INSERT INTO t2 VALUES (1),(2),(3),(4),(5);SELECT t1.id1, GROUP_CONCAT(t1.id2 ORDER BY t1.id2 ASC) AS concat_id FROM t1, t2 WHERE t1.id1 = t2.id1 AND t1.id1=1 GROUP BY t1.id1;id1 concat_id
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -