2014-09-01 54 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)