【发布时间】:2013-07-24 21:43:12
【问题描述】:
This 是我的小提琴。
表和数据是
create table Table3 (MatchID varchar(10), ItemType varchar(10));
insert into Table3 values
('M001','Fruit'),
('M001','Animal'),
('M002','Fruit'),
('M002','Vehicle');
当您有一个按 MatchID 和 ItemType 排序的选择查询时,它正在返回
select MatchID,ItemType from Table3 order by MatchID,ItemType;
MATCHID ITEMTYPE
M001 Animal
M001 Fruit
M002 Fruit
M002 Vehicle
像这样,这是意料之中的,也是正确的。
但是,当我 group_concated 时,它并没有以有序的方式返回。
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
。为什么 group_concat 的行为如此?如何产生预期的输出?
【问题讨论】:
标签: mysql sql group-by sql-order-by group-concat