Group data from a Query?

1

I do a GROUP BY in a query result and the result comes like this:

Iwouldliketoknowifitispossibletobringgroupedinthisway?

I'd like to know if this collation is best done in PHP or Query and how can it be done?

Follow my code:

SQL Query

SELECT id,cliente, descricao, forma_pgto, bandeira, valor, codigo, data, cod_aut
FROM ItensVendidos
WHERE data BETWEEN '2017-05-22' AND '2017-05-22'
GROUP BY id,cliente, descricao, forma_pgto, bandeira, valor, codigo, data, cod_aut
    
asked by anonymous 22.05.2017 / 18:22

1 answer

0

You can use GROUP_CONCAT . , grouping by COD_AUT , if this is the only common parameter.

Logo:

SELECT id, 
       cliente, 
       GROUP_CONCAT(descricao) as descricao, 
       forma_pgto, 
       bandeira, 
       valor, 
       codigo, 
       data, 
       cod_aut 
FROM   itensvendidos 
WHERE  data BETWEEN '2017-05-22' AND '2017-05-22' 
GROUP  BY cod_aut 

This will return as " SALES 1, SALES 2, SALES 3, SALES 4 ", if you want to display them without a comma, you can use the SEPARATOR of MySQL ( documentation ) and choose the tab you prefer. You can also handle this in PHP using str_replace / strtr or use explode to create an array.

    
22.05.2017 / 18:32