EliasConx
EliasConx

Reputation: 3

IN MYSQL, SUM ALL VALUES IN

I've got a table called 'articulo' and column called CostoPromedio, I Want to SUM this column by the id , I'm getting this ID's from PHP, I'm inserting these on the query:

INSERT INTO dbName.venta (tMoneda,idTipoDeDocumento,total) VALUES(11,32,(SELECT SUM(CostoPromedio) FROM dbName.articulo WHERE idArticulo IN (15, 16,15,17,16))

The problem is is adding 15,16,17, but in the repeated ID is not adding the result, is just ignoring him, I want adding all, including if is the same ID.

Sorry about my english, is not my native language

Upvotes: 0

Views: 85

Answers (2)

chetan
chetan

Reputation: 2886

I think you've missed a braket at last. And remove duplicates in 'IN' clause.

INSERT INTO dbName.venta (tMoneda,idTipoDeDocumento,total) 
VALUES(11,32,(SELECT SUM(CostoPromedio) 
              FROM dbName.articulo 
              WHERE idArticulo IN (15,16,17)));

Upvotes: 0

Taryn
Taryn

Reputation: 247640

You will want to use INSERT INTO ..SELECT...FROM as the query:

INSERT INTO dbName.venta (tMoneda,idTipoDeDocumento,total) 
SELECT 11, 32, SUM(CostoPromedio) 
FROM dbName.articulo
WHERE idArticulo IN (15, 16, 17)

Upvotes: 3

Related Questions