如何嵌套GROUP_CONCAT?

时间:2013-08-18 16:45:12

标签: android sqlite nested concatenation

我有这张桌子:

enter image description here

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

返回:

enter image description here

如何以这种方式获得结果 - 按入口和感觉分组并按分号';'分组标志?' enter image description here

1 个答案:

答案 0 :(得分:2)

首先,按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