开发者

Need specific mysql statement to get master word and all words in one select

I have a table like:

ID   word   masterWord
1    test   1
1    test1  0
1    test2  0
2    look   0
2    kiss   1
2    eye    0

Now I'd like to get from select statement 2 records with:

test      test1, 开发者_开发百科test2
kiss      look, eye

First column is master word, second column in group_concat all the other words.

Master words are marked with true (1) in the table and the ID's are declaration which words holds a group (of synonyms). Ok, I know I've made stupid words but I am making this for non English language that has one word has different endings depending on it's count, gender etc...

Every ID MUST have one word selected as master word... if that helps query or not I don't know ;-)


I think something like

SELECT MAX(CASE WHEN masterWord = 1 THEN word END) AS masterWord ,
       GROUP_CONCAT(CASE WHEN masterWord = 0 THEN word END) AS OtherWords
FROM YourTable       
GROUP BY ID

Should do it. Perhaps you might need to add a CAST to the GROUP_CONCAT input as well.


select m.word, group_concat(w.word) wordlist
from words m
left join words w on w.masterword=0 and w.id=m.id
where m.masterword=1
group by m.id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜