sql left join,您将右侧字段实例组合到单个csv列结果中 [英] sql left join where you combine right side field instances into a single csv column result

查看:55
本文介绍了sql left join,您将右侧字段实例组合到单个csv列结果中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有sql左联接,其中每个联接都有多个右侧命中.相反,如果为每个右侧匹配项返回一行,我想返回一行,其中多个右侧匹配项合并为csv [逗号分隔值结果].

I have sql left join where there is are multiple right side hits for every join. Instead if returning a row for each right side match i'd like to return a single row where the multiple right side matches are combined into a csv [ comma separated value result ].

-- instead of this that produces a left.id row for every associated tag entry . . .
select left.id, left.name, right.tag from support_request left 
join support_request_tags right on left.id = right.support_request_id

-- need something like this that produces one left.id row with csv column of all associated tag entries . . .
select left.id, left.name, right.<csv list of all associated tags ???> from support_request left 
join support_request_tags right on left.id = right.support_request_id

推荐答案

SELECT left.id, 
       left.name, 
       GROUP_CONCAT(right.tag) AS tags_CSV_list 
FROM support_request left 
JOIN support_request_tags right ON left.id = right.support_request_id
GROUP BY left.id, 
         left.name;

这篇关于sql left join,您将右侧字段实例组合到单个csv列结果中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆