2014-09-01 61 views
2

我需要標籤從下面的查詢相結合,用逗號分隔符分隔:結合SQL結果用逗號分隔符

SQL

select question.text,tag.text 
from question 
left join q_t on question.id = q_t.wall_id 
left join tag on q_t.tag_id = tag.id 
where question.id in (1000001,1000002,1000003,1000004,1000005) 
order by field(question.id,1000001,1000002,1000003,1000004,1000005) 

當前的結果:

text       text 
where is England?   Geography 
where is England?   Continent 
where is England?   general_knowledge 
how many ...?    sport 
how many ...?    Europe 

請求的結果:

text       text 
where is England?   Geography,Continent,general_knowledge 
how many ...?    sport,Europe 

感謝,

回答

3

您可以使用group_concat但有1024個字符的默認限制約束,從結果來連接和剩餘的數據將通過以下手冊被截斷但是這個限制,可以增加,但它也有一個依賴max_allowed_packet too

select question.text,group_concat(tag.text) 
from question 
left join q_t on question.id = q_t.wall_id 
left join tag on q_t.tag_id = tag.id 
where question.id in (1000001,1000002,1000003,1000004,1000005) 
group by question.text 
order by field(question.id,1000001,1000002,1000003,1000004,1000005)