Pregunta

I have these tables

Table_1
number...letters...name
1.....AB...Peter
2...BC...Paul
3...AC...Mary

Table_2
letter...names
A...NULL
B...NULL
C...NULL

and I have to update Table_2 into this

Table_2
letter...names
A...Peter, Mary
B...Peter, Paul
C...Paul, Mary

All are varchar except for the number row. It concatenates all names that has a specific letter on their 'letters' row. How can I do this with just one query in sql?

¿Fue útil?

Solución

You can do this with a subquery in the update:

update table2
    set names = (select group_concat(t1.name order by number separator ', ')
                 from table1 t1
                 where t1.letters like concat('%', table2.letter, '%')
                );
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top