2015-02-06 64 views
0

我有一個存儲過程返回過程執行代碼在MySQL

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetNotExecutedOrders`(IN contractID INT(11)) 
BEGIN 
SELECT idContract, idOrder, ProductID, Quantity, (SUM(`order`.Quantity))*(product.Price) as 'Total amount' 
FROM typography.contract 
JOIN typography.`order` ON contract.idContract=`order`.ContractID 
JOIN typography.product ON `order`.ProductID=product.idProduct 
WHERE idContract=contractID and ExecutionDate IS NULL 
GROUP BY idOrder 
ORDER BY idOrder; 
END 

我需要那麼它是否有與該合同ID沒有合同返回的執行代碼<> 0來解決它,和合同列表如果與該合同ID有合同,則執行代碼= 0。

+0

使用'IF'語句。 – Barmar 2015-02-06 00:47:13

+0

是的,我試過,但我不知道如何返回代碼本身。你能幫我解決嗎? – 2015-02-06 00:49:21

+0

我建議爲代碼添加一個'OUT'參數。 – Barmar 2015-02-06 00:53:46

回答

0

使用代碼的OUT參數。然後使用COUNT(*)查詢進行設置。

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetNotExecutedOrders`(IN contractID INT(11), OUT executionCode INT) 
BEGIN 

    SELECT COUNT(*) > 0 INTO executionCode 
    FROM typography.contract 
    JOIN typography.`order` ON contract.idContract=`order`.ContractID 
    JOIN typography.product ON `order`.ProductID=product.idProduct 
    WHERE idContract=contractID and ExecutionDate IS NULL; 

    IF (executionCode) 
     SELECT idContract, idOrder, ProductID, Quantity, (SUM(`order`.Quantity))*(product.Price) as 'Total amount' 
     FROM typography.contract 
     JOIN typography.`order` ON contract.idContract=`order`.ContractID 
     JOIN typography.product ON `order`.ProductID=product.idProduct 
     WHERE idContract=contractID and ExecutionDate IS NULL 
     GROUP BY idOrder 
     ORDER BY idOrder; 
    END IF; 
END 
+0

非常感謝!但是,你能解釋一下COUNT(*)> 0是如何工作的嗎?我不明白爲什麼它可以在沒有IF語句的情況下使用 – 2015-02-06 01:20:45

+0

比較函數和運算符計算爲'true'或'false',在MySQL中表示爲'1'和'0'。 – Barmar 2015-02-06 01:21:24

相關問題