1
When I try to add the COUNT it returns error, but when I shoot, the script works normal. Follow the error:
Invalid use of group function
Script:
SELECT DISTINCT 
        TB_PRODUTOS.ID_PRODUTO,
        CONVERT(
            GROUP_CONCAT(
                DISTINCT CONCAT(
                    TB_PRODUTOS.NOME_PRODUTO,
                    "(", COUNT(TB_PRODUTOS.ID_PRODUTO), ")"
                ) 
                SEPARATOR ', '
            ) 
            USING 'utf8'
        ) AS NOME_PRODUTO,      
        TB_ITEM_PEDIDO_PRODUTO.ID_PEDIDO,
        TB_ITEM_PEDIDO_PRODUTO.ID_PRODUTO
    FROM TB_ITEM_PEDIDO_PRODUTO     
    INNER JOIN TB_PEDIDO ON TB_PEDIDO.ID_PEDIDO = TB_ITEM_PEDIDO_PRODUTO.ID_PEDIDO  
    INNER JOIN TB_PRODUTOS ON TB_PRODUTOS.ID_PRODUTO = TB_ITEM_PEDIDO_PRODUTO.ID_PRODUTO
    WHERE TB_PEDIDO.ID_PESSOA = 1
    GROUP BY TB_PEDIDO.ID_PEDIDO
I want that result returned:
Produto
--------------------------------
produto(2), testando(1), teste(4)
I followed this website:
http://www.percona.com/blog/2013/10/22/the-power-of-mysqls-group_concat/
Database:
TB_PEDIO:

TB_ITEM_PEDIDO_PRODUTO:

TB_ITEM_PEDIDO_PRODUTO_INGREDIENTE:

TB_PRODUTO:

TB_PRODUTOS_INGREDITES:

TB_INGREDITES:

I think an order by GROUP_CONCAT( DISTINCT CONCAT( TB_PRODUTOS.NOME_PRODUTO, "(", COUNT(TB_PRODUTOS.ID_PRODUTO), ")" ) ORDER BY TB_PRODUTOS.ID_PRODUTO SEPARATOR ', 
'; )
– Pedro Rangel
@Pedrorangel, it’s not the
order by:/– Leonardo
Voce could show me your test base with the respective tables?
– Pedro Rangel
@Pedrorangel, Updated!
– Leonardo