我需要组合以下查询中的标签,并用逗号分隔符分隔:
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
谢谢
最佳答案
您可以使用group_concat
但从结果连接有 1024 个字符的默认限制,剩余数据将被截断,但是可以通过以下手册增加此限制,但它也依赖于 max_allowed_packet也是
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)
关于mysql - 将 SQL 结果与逗号分隔符结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25603030/