Adding decimal values mysql

0

I made a query that adds a decimal field, but I'm having a problem, because when I put the group by t1.id_sell clause it lists the sales but no longer adds correctly and without the correct sum clause but only returns one line.

query:

SELECT Sum(t1.preco_sell) AS result_sum, t1.*, t2.name, t3.name_produto 
FROM   tb_sell t1 
       INNER JOIN tb_user t2 
               ON t1.id_user = t2.id_user 
       INNER JOIN tb_produtos t3 
               ON t1.id_produto = t3.id_produto 
WHERE  t1.status = 2 
GROUP  BY t1.id_sell 
    
asked by anonymous 30.03.2017 / 18:09

1 answer

0

The problem is with yours in the fields that you display in your Select , in your junction in Group By

If you wanted to display the sum of all items sold in a new column, what you should do is a SubQuery for this

SELECT t1.*, t2.name, t3.name_produto,
(SELECT Sum(preco_sell)
    FROM   tb_sell
    WHERE  status = 2
)AS result_sum
FROM   tb_sell t1
       INNER JOIN tb_user t2
               ON t1.id_user = t2.id_user
       INNER JOIN tb_produtos t3
               ON t1.id_produto = t3.id_produto
WHERE  t1.status = 2

When you send all the items in t1 the query will not be able to group, since all the fields probably have different values and thus are not returning what you want

>     
30.03.2017 / 18:21