Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 按内部组订购\u concat_Mysql_Sql_Group By_Sql Order By_Group Concat - Fatal编程技术网

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;