Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
USING STORED PROCEDURE to SUM, GROUP BY and insert to another table with

I have created 3 tables: item, shop and stock. Plus a stored procedure called inserting which inserts to the shop table with a given item from the item table

CREATE TABLE item(
i_id int(11) auto_increment,
i_name varchar(255) not null,
primary key(i_id));

CREATE TABLE shop(
s_id     int(11) auto_increment,
s_name   varchar(255) not null,
s_item   int(11) not null,
s_qty    int(11) not null,
primary  key(s_id),
foreign  key(s_item) references item(i_id)
);

CREATE TABLE stock(
item     int(11) not null,
total    int(11) not null
);

CREATE PROCEDURE inserting (
IN shop_name varchar(225),
IN shop_item int(11),
IN shop_qty int(11)
)

BEGIN

INSERT INTO shop(s_name, s_item, s_qty) 
VALUES
(shop_name, shop_item, shop_qty);

INSERT INTO STOCK(item, total) 
SELECT s_item, SUM(s_qty) FROM shop GROUP BY s_item
ON DUPLICATE KEY UPDATE
item = VALUES(item),
total = VALUES(total);

The first insert works, but on the second insert when it populates the stock table it gives me extra columns, which i'm not expecting.

I have tried using REPLACE INTO and ON DUPLICATE KEY UPDATE to get single results, still the results comes as the following:

SELECT * FROM `stock`;
+------+-------+
| ITEM | TOTAL |
+------+-------+
|    1 |     5 |
|    1 |     9 |
+------+-------+

what I am trying to achieve is, group the ITEM column, and sum up the TOTAL to a single row.

what am I doing wrong here, or missing from the query?

thanks.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

For the on duplicate key syntax to work as expected, you need a unique or primary key constraint on the target table, so the database can identify the "duplicate" rows. Same goes for the REPLACE syntax.

But your stock table does not have a primary key. Consider the following DDL instead:

CREATE TABLE stock(
    item     int(11) primary key,
    total    int(11) not null
);

Side note: there is no need to reassign column item in the on duplicate key clause, since it's what is used to identify the conflict in the first place. This is good enough:

INSERT INTO STOCK(item, total) 
SELECT s_item, SUM(s_qty) FROM shop GROUP BY s_item
ON DUPLICATE KEY UPDATE total = VALUES(total);
over 4 years ago · Santiago Trujillo Relatório

0

If you run this one time, it should work as you expected. But subsequent runs may bring duplicate ITEM because of what @gmb said. The table must have a UNIQUE index or PRIMARY KEY. See more details here

https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda