Group data from a Query?

Asked

Viewed 241 times

1

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

inserir a descrição da imagem aqui

I wonder if it is possible to bring together in this way? inserir a descrição da imagem aqui

I wonder if this grouping is best done in PHP or Query and how it can be done?

Follows my code:

Query SQL

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
  • A Function to the description, or a subconsulta would solve your problem.see

  • 1

    I will remove the PHP code from your question to make it clearer, all right?

  • OK @Marconi no problem!

1 answer

0

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

Soon:

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 show them without comma you can use the SEPARATOR mysql (documentation) and choose the tab you prefer. You can also treat this in PHP using str_replace/strtr or use explode to create an array.

  • The Grup_Contat Works on Sql-Server @Inkeliz?

  • I saw the tag mysql, so I answered, now I don’t know if you own or not, maybe you have some equivalent resource.

  • Now that I’ve noticed too, he’s tagged Mysql and Sql-Server.

  • I found a question here

  • I’m sorry I accidentally put Mysql is on sql server. I just tried and Group_concat does not work on SQL Server

Browser other questions tagged

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