Converting columns into rows by concatenating the results

Asked

Viewed 19 times

2

I have the following select :

SELECT pedido, item FROM pedidos

How do I get the result of it in columns and step to row concatenating with -? Example:

Upshot

pedido    item
1         batata
1         arroz
1         feijão
1         tomate

Expected:

pedido   item 
1         batata - arroz - feijão - tomate

1 answer

3


Use GROUP_CONCAT:

SELECT pedido, GROUP_CONCAT(item SEPARATOR ' - ') as item FROM pedidos

Test in Sqlfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.