2013-08-18 32 views
1

我有這個表:如何嵌套GROUP_CONCAT?

enter image description here

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

回報:

enter image description here

如何獲得導致這種方式,分組由輸入和意義之間用分號 ';' 隔開標誌?' enter image description here

回答

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