Reputation: 3
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
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
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