Domanda

I've a query to find all the notes for a lead in sugarcrm. As per the screenshot a lead have 3 or 4 notes.

I would like to concatenate all the notes in a single row (name column) based on lead's ID.

My query is,


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

È stato utile?

Soluzione

You can use group_concat() function

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

Altri suggerimenti

You need to use GROUP_CONCAT .

In your case, use the following syntax for a comma separated list :

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

This is typed out of my head, it may need refinings.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top