根据潜在客户的ID将所有注释连接在一行中?

时间:2014-03-14 07:44:23

标签: mysql sql sugarcrm

我有一个查询来查找糖块中铅的所有注释。根据屏幕截图,一个主管有3或4个音符。

我想根据潜在客户的ID连接一行(名称列)中的所有注释。

我的查询是,


SELECT l.first_name, l.last_name, l.id, n.name, n.description
FROM 
    leads AS l inner join notes as n on l.id = n.parent_id
where l.deleted = 0
and n.deleted = 0
and l.id='104c4b25-adab-32f3-16ee-50d098a5dd5d'

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以使用group_concat()功能

SELECT l.first_name, l.last_name, l.id, group_concat(n.name), n.description
FROM 
    leads AS l inner join notes as n on l.id = n.parent_id
where l.deleted = 0
and n.deleted = 0
and l.id='104c4b25-adab-32f3-16ee-50d098a5dd5d'
group by l.id

答案 1 :(得分:1)

您需要使用GROUP_CONCAT

在您的情况下,请使用以下语法作为逗号分隔列表:

SELECT l.first_name, l.last_name, l.id, n.name, n.description,
GROUP_CONCAT(n.name SEPARATOR ",") AS notes
FROM 
leads AS l inner join notes as n on l.id = n.parent_id
where l.deleted = 0
and n.deleted = 0
and l.id='104c4b25-adab-32f3-16ee-50d098a5dd5d'
GROUP BY l.id

这是我脑子里打字的,可能需要精炼。