2016-06-07 1 views
1

존재하지 않는 경우 : 당신이의 첫 4 행 위의 이미지에 표시되는 내용으로데이터 전송 데이터가 내가 MySQL의에서이 개 테이블을 가지고 그들이

enter image description here

테이블 General InventoryPurchase Order의 필드 Item Code은 같으며 테이블 Purchase Order의 다섯 번째 행은 테이블 General Inventory에 없습니다. 이제 내 질문은 어떻게이 일을 성취 할 수 있는가? 이 필드 Item Code에서 데이터를 이전하는 것은 동일하며,이 마지막 하나 때문에 (아래 이미지)는 필드 테이블 General InventoryQuantityPurchase Order에서 데이터, 위의 이미지에서 보는 것과

enter image description here

는 SUM있다 General Inventory 테이블에 동일한 값이 추가되지 않았습니다.

질문을 짧게 줄이십시오. Item Code 분야에서이 데이터 Purchase Order에서 데이터 General Inventory에 존재하지 않는 경우 다른 같은 새로운 데이터를 추가 할 경우 나는 General QuantityPurchase Order 분야 Quantity에서 데이터를 요약 할 수 있습니까

.

이 코드를 시도했습니다.

UPDATE GeneralInventory GI 
INNER JOIN PurchaseOrder PO ON GI.ItemCode = PO.ItemCode AND GI.`Description` = PO.`Description` AND GI.Quantity = PO.Quantity 
SET GI.Quantity = GI.Quantity + PO.Quantity 

는 출력이

enter image description here

가 지금은 출력이 상기 제 2 코드를

INSERT INTO GeneralInventory (ItemCode, `Description`, Quantity) 
SELECT PO.ItemCode, PO.`Description`, PO.Quantity 
FROM PurchaseOrder PO 
LEFT JOIN GeneralInventory GI ON GI.ItemCode = PO.ItemCode AND GI.`Description` = PO.`Description` AND GI.Quantity = PO.Quantity 
WHERE GI.ItemCode IS NULL 

을 시도합니다.

enter image description here

데이터 대신 반복되어 상기 Item 5 첨가한다.

답변

1

먼저 당신이 GeneralInventory 테이블 그리고

INSERT INTO GeneralInventory (ItemCode, `Description`, Quantity) 
SELECT PO.ItemCode, PO.`Description`, PO.Quantity 
FROM PurchaseOrder PO 
LEFT JOIN GeneralInventory GI ON GI.ItemCode = PO.ItemCode AND GI.`Description` = PO.`Description` AND GI.Quantity = PO.Quantity 
WHERE GI.ItemCode IS NULL 

당신이 테이블

UPDATE GeneralInventory GI 
INNER JOIN PurchaseOrder PO ON GI.ItemCode = PO.ItemCode AND GI.`Description` = PO.`Description` AND GI.Quantity = PO.Quantity 
SET GI.Quantity = GI.Quantity + PO.Quantity 
+0

에게 가입으로 GeneralInventory의 수량 값을 업데이트 할 수 있습니다에 일치하지 않는 행을 삽입 얻을 LEFT JOIN을 할 수 있습니다 사용하는 것은 확인 이니셜을 사용하는 것입니다 현장 선생님? 왜 PO와 GI를 사용합니까? 그것을 사용하는 것이 좋습니다? –

+0

당신은 선생님이 내가이 두 MYSQL 명령을 사용할 것임을 의미합니다. 둘 다 실행할 것입니까? –

+0

예, 두 명령을 사용하여 기대치를 달성 할 수 있습니다. 그 이상은 이니셜이 아니며 [ALIASES] (http://www.techonthenet.com/mysql/alias.php)로 불립니다. 더 나은 가독성을 위해 테이블 ​​별칭을 사용했습니다. – Arulkumar

관련 문제