我有这张桌子:



GROUP_CONCAT(DISTINCT mytable.gloss) AS gloss
...
GROUP BY mytable.entry


返回:



如何以这种方式获得结果-按输入和意义分组,并用分号';'分隔标志?'

最佳答案

首先,按sense分组:

SELECT entry,
       sense,
       GROUP_CONCAT(DISTINCT gloss)
FROM mytable
GROUP BY entry,
         sense

entry sense gloss
----- ----- ------------
1     1     Orange,Red
1     2     Blue
2     3     Green
2     4     Yellow,Ivory
3     5     Grey


然后对该结果运行另一个GROUP BY

SELECT entry,
       MIN(sense) AS sense,
       GROUP_CONCAT(gloss, ';') AS gloss
FROM (SELECT entry,
             sense,
             GROUP_CONCAT(DISTINCT gloss) AS gloss
      FROM mytable
      GROUP BY entry,
               sense)
GROUP BY entry

entry sense gloss
----- ----- ------------------
1     1     Orange,Red;Blue
2     3     Green;Yellow,Ivory
3     5     Grey

10-06 05:28