Mysql 按内部组订购\u concat
这是我的小提琴 表和数据是Mysql 按内部组订购\u concat,mysql,sql,group-by,sql-order-by,group-concat,Mysql,Sql,Group By,Sql Order By,Group Concat,这是我的小提琴 表和数据是 create table Table3 (MatchID varchar(10), ItemType varchar(10)); insert into Table3 values ('M001','Fruit'), ('M001','Animal'), ('M002','Fruit'), ('M002','Vehicle'); 当您有一个按MatchID和ItemType排序的select查询时,它将返回 select MatchID,ItemType from
create table Table3 (MatchID varchar(10), ItemType varchar(10));
insert into Table3 values
('M001','Fruit'),
('M001','Animal'),
('M002','Fruit'),
('M002','Vehicle');
当您有一个按MatchID和ItemType排序的select查询时,它将返回
select MatchID,ItemType from Table3 order by MatchID,ItemType;
MATCHID ITEMTYPE
M001 Animal
M001 Fruit
M002 Fruit
M002 Vehicle
像这样,这是意料之中的,也是正确的
然而,当我分组时,它并没有以有序的方式返回
Select group_concat(ItemType) as typesTomatch ,MatchID
from (select MatchID,ItemType from Table3
order by MatchID,ItemType)
c group by MatchID;
它又回来了
TYPESTOMATCH MATCHID
Fruit,Animal M001
Fruit,Vehicle M002
出乎意料
TYPESTOMATCH MATCHID
Animal,Fruit M001
Fruit,Vehicle M002
。为什么concat集团的行为如此?如何产生预期的输出?尝试
按内部排序
看你救了我一天。。不知道集团内部的订单情况。\u CONCAT()。太酷了你也踢出了一个结果集+共1人
SELECT GROUP_CONCAT(ItemType ORDER BY ItemType) AS typesTomatch , MatchID
FROM Table3 GROUP BY MatchID;