See this sqlfiddle (also below should the fiddle die in future). The goal is to return all checked out items back to the items table, updating the quantity value.
CREATE TABLE checkout(
id INT NOT NULL
-- toUser, indexes, etc
);
CREATE TABLE inventory(
id INT NOT NULL AUTO_INCREMENT,
quantity INT DEFAULT 0,
PRIMARY KEY (id)
);
INSERT INTO inventory(quantity) VALUES(90);
INSERT INTO inventory(quantity) VALUES(42);
-- 10 values
INSERT INTO checkout(id) VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1);
-- 8 values
INSERT INTO checkout(id) VALUES(2),(2),(2),(2),(2),(2),(2),(2);
-- Return all the checked out items back to the inventory (up the quantity)
UPDATE inventory i
INNER JOIN checkout c ON c.id = i.id
SET i.quantity = i.quantity + 1;
After this UPDATE, I was expecting this result:
Inventory:
id = 1, quantity = 100
id = 2, quantity = 50
Instead, this was received:
Inventory:
id = 1, quantity = 91
id = 2, quantity = 43
A join returns 18 rows:
SELECT * FROM inventory i INNER JOIN checkout c ON c.id = i.id;
I believe there is a hole in my understanding of the UPDATE operation. Thanks for your help.