我不確定要了解您的需求,但我會嘗試解釋: 如果在您的表格示例中mat_id 1和3完全相同(相同品牌和EAN條形碼),則表格模型不正確,最好能夠做出不同的表(產品和供應商,第三個參考哪些材料可以由供應商/供應商,其可以發送的產品和分娩史來發送。
CREATE TABLE IF NOT EXISTS `DELIVERY_HISTORY` (
`dh_id` int(11) NOT NULL AUTO_INCREMENT,
`shm_id` int(11) NOT NULL,
`delivery_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`stock_in` int(11) NOT NULL,
PRIMARY KEY (`dh_id`),
KEY `shm_id` (`shm_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `MATERIAL` (
`material_id` int(11) NOT NULL AUTO_INCREMENT,
`material_name` varchar(64) DEFAULT NULL,
`stock_bal` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`material_id`),
KEY `material_name` (`material_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `SUPPLIER` (
`supplier_id` int(11) NOT NULL AUTO_INCREMENT,
`supplier_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`supplier_id`),
KEY `supplier_name` (`supplier_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `SUPPLIER_HAS_MATERIAL` (
`shm_id` int(11) NOT NULL AUTO_INCREMENT,
`supplier_id` int(11) NOT NULL,
`material_id` int(11) NOT NULL,
PRIMARY KEY (`shm_id`),
KEY `supplier_id` (`supplier_id`,`material_id`),
KEY `material_id` (`material_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `DELIVERY_HISTORY`
ADD CONSTRAINT `DELIVERY_HISTORY_ibfk_1` FOREIGN KEY (`shm_id`) REFERENCES `SUPPLIER_HAS_MATERIAL` (`shm_id`);
ALTER TABLE `SUPPLIER_HAS_MATERIAL`
ADD CONSTRAINT `SUPPLIER_HAS_MATERIAL_ibfk_2` FOREIGN KEY (`material_id`) REFERENCES `MATERIAL` (`material_id`),
ADD CONSTRAINT `SUPPLIER_HAS_MATERIAL_ibfk_1` FOREIGN KEY (`supplier_id`) REFERENCES `SUPPLIER` (`supplier_id`);
在這種情況下,您可以創建多個物料和供應商,參考哪些物料由哪個供應商提供,或只需提供供應商可以提供的所有產品的清單以及供應商交付的歷史記錄。
現在添加材料:
INSERT INTO `test`.`MATERIAL` (`material_id` , `material_name` , `stock_bal`) VALUES (NULL , 'paint', '0'), (NULL , 'bolt', '0');
添加供應商:
INSERT INTO `test`.`SUPPLIER` (`supplier_id` , `supplier_name`)
VALUES
(NULL , 'dummy'),
(NULL , 'test'),
(NULL , 'demo');
添加引用材料/供應商
INSERT INTO `SUPPLIER_HAS_MATERIAL`
(`shm_id`, `supplier_id`, `material_id`)
VALUES
(NULL, 1, 1),
(NULL, 2, 2),
(NULL, 3, 1);
顯示一些utils的疑問: 列出所有材料和供應商:
SELECT m.material_name, s.supplier_name
FROM MATERIAL m, SUPPLIER s, SUPPLIER_HAS_MATERIAL shm
WHERE shm.supplier_id = s.supplier_id
AND shm.material_id = m.material_id
列出所有供應商的特定材料:
SELECT m.material_name, s.supplier_name
FROM MATERIAL m, SUPPLIER s, SUPPLIER_HAS_MATERIAL shm
WHERE shm.supplier_id = s.supplier_id
AND shm.material_id = m.material_id
AND m.material_id = 1
列出所有材料的特定供應商:
SELECT m.material_name, s.supplier_name
FROM MATERIAL m, SUPPLIER s, SUPPLIER_HAS_MATERIAL shm
WHERE shm.supplier_id = s.supplier_id
AND shm.material_id = m.material_id
AND s.supplier_id = 1
顯示產品庫存:
SELECT m.material_name, m.stock_bal
FROM MATERIAL m
WHERE m.material_id = 1
從這裏如果你使用PHP,我認爲你知道stock_in,supplier和material的價值。
查找材料/供應商的在SUPPLIER_HAS_MATERIAL的ID(material_id = 1:漆; supplier_id = 1:虛設)
SELECT shm.shm_id
FROM MATERIAL m, SUPPLIER s, SUPPLIER_HAS_MATERIAL shm
WHERE shm.supplier_id = s.supplier_id
AND shm.material_id = m.material_id
AND m.material_id =1
AND s.supplier_id =1
在我的情況shm_id = 1。現在是時候添加的遞送(stock_in = 20):
INSERT INTO `test`.`DELIVERY_HISTORY` (
`dh_id` , `shm_id` , `delivery_date` , `stock_in`
)
VALUES (
NULL , 1, CURRENT_TIMESTAMP , 20
)
立即更新的材料stock_bal與材料塗料(material_id = 1)
UPDATE MATERIAL
SET stock_bal = (stock_bal + 20)
WHERE material_id = 1
stock_in(20的值)
請注意在PHP中,您可以在INSERT後檢查mysqli_insert_id函數的值。如果值> 0,則INSERT正確完成,否則您可以編寫後備例程。
你現在可以檢查你的油漆stock_bal的價值。
這裏有2最後INSERT和UPDATE完成你的測試用例,但不要忘了使用select之前瞭解我是怎麼shm_id:
-- shm_id = 2 : material_id = 2 (bolt)/supplier_id = 2 (test)
-- Adding 10 bolt
INSERT INTO `test`.`DELIVERY_HISTORY` (
`dh_id` , `shm_id` , `delivery_date` , `stock_in`
)
VALUES (
NULL , 2, CURRENT_TIMESTAMP , 10
);
UPDATE MATERIAL
SET stock_bal = (stock_bal + 10)
WHERE material_id = 2;
-- CHECK YOUR MATERIAL stock_bal
-- shm_id = 3 : material_id = 1 (paint)/supplier_id = 3 (demo)
-- Adding 10 paint
INSERT INTO `test`.`DELIVERY_HISTORY` (
`dh_id` , `shm_id` , `delivery_date` , `stock_in`
)
VALUES (
NULL , 2, CURRENT_TIMESTAMP , 10
);
UPDATE MATERIAL
SET stock_bal = (stock_bal + 10)
WHERE material_id = 1;
-- CHECK YOUR MATERIAL stock_bal
我希望這個解釋會幫助你(如果我已經正確理解你的問題)。
您不應該將其作爲數據庫中的表格,您應該將其計算爲'select material_name,sum(stock_in)from inventory group by material_name' – Barmar
我要編輯我的帖子 – user3117337
Can我問一個小提琴先生?請? – user3117337