2014-03-14 34 views
2

我有一個查詢,以查找所有在sugarcrm鉛的筆記。根據屏幕截圖,潛在客戶有3或4個註釋。根據潛在客戶ID將所有註釋連接在一行中?

我想根據主角的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

回答

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

您需要使用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 

這是輸入了我的頭,它可能需要refinings。

相關問題