this_proc:begin
START TRANSACTION;
BEGIN
IF amount <= 0 OR amount >= 99 THEN
LEAVE this_proc;
END IF
IF EXISTS (SELECT `amount` AS AM FROM `storage_items` WHERE SI.`storage_id` = from_storage AND SI.`item_id` = item_id) THEN
IF AM < amount THEN
LEAVE this_proc;
ELSE IF AM = amount THEN
DELETE FROM `storage_items` WHERE SI.`storage_id` = from_storage AND SI.`item_id` = item_id;
ELSE
UPDATE `storage_items` as SI SET SI.`amount` = SI.`amount` - amount WHERE SI.`storage_id` = from_storage AND SI.`item_id` = item_id;
END IF
ELSE
LEAVE this_proc;
END IF
IF EXISTS (SELECT `amount` AS AM FROM `storage_items` WHERE SI.`storage_id` = into_storage AND SI.`item_id` = item_id) THEN
UPDATE `storage_items` as SI SET SI.`amount` = SI.`amount` + amount WHERE SI.`storage_id` = into_storage AND SI.`item_id` = item_id;
ELSE
INSERT INTO `storage_items` (`item_id`, `storage_id`, `amount`) VALUES (item_id, into_storage, amount);
END IF
END
COMMIT WORK
END
Alles anzeigen
Tabelle dazu (Funktionsfähig):
CREATE TABLE `storage_items` (
`item_id` INT(11) NOT NULL,
`storage_id` INT(11) NOT NULL,
`amount` INT(11) NOT NULL DEFAULT '1',
INDEX `FK__items` (`item_id`) USING BTREE,
INDEX `FK__storages` (`storage_id`) USING BTREE,
CONSTRAINT `FK__items` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `FK__storages` FOREIGN KEY (`storage_id`) REFERENCES `storages` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COMMENT='all item informations'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
Alles anzeigen