How to put two records from one table in another as a single in select MySql Bank

0

How to put two records from one table in another as a single MySql select bank?

SELECT TURMA.turma_id, TURMA.turma_nome, PROF.prof_name 
from turma TURMA LEFT JOIN professor_materia PROF_MAT 
ON TURMA.turma_id=PROF_MAT.prom_mat LEFT JOIN professor PROF 
ON PROF_MAT.prom_prof=PROF.prof_id

it is returning like this:

I would like it to return like this:

turma_id   |  turma_nome     |  prof_name
1          |  1º ANO A       |  PROFESSOR DE MATEMATICA, DANIEL
2          |  2º ANO         |  NULL

Thank you in advance.

    
asked by anonymous 02.09.2017 / 21:44

1 answer

1

In this case, just use GROUP BY with the function GROUP_CONCAT , eg:

SELECT
    MIN(TURMA.turma_id) AS turma_id,
    TURMA.turma_nome,
    GROUP_CONCAT(PROF.prof_name SEPARATOR ', ') AS prof_name
FROM
    turma TURMA
    LEFT JOIN professor_materia PROF_MAT ON TURMA.turma_id = PROF_MAT.prom_mat
    LEFT JOIN professor PROF ON PROF_MAT.prom_prof=PROF.prof_id
GROUP BY
    TURMA.turma_nome

The GROUP BY groups the result and the GROUP_CONCAT concatenates the grouped results of a column.

To read more about GROUP BY and GROUP_CONCAT : link

    
02.09.2017 / 21:50