在 MySQL 中嵌套 IF ELSE

Nested IF ELSE in MySQL

我想执行下面的存储过程,但是它给我一个错误。更新订单 table 后在标志 1 内,我想检查另一个条件,如果这是真的,那么我想 运行 另一个更新查询。我在 SQL 服务器中使用临时 table 尝试了这个并且它有效。请帮我。

错误如下:

Script line: 4 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM product PR INNER JOIN (SELECT PR.ID AS ProductID, (PR.Quantity - OD.Qu' at line 35

DELIMITER $$

DROP PROCEDURE IF EXISTS `onlineshop`.`USP_Public_SaveOrder` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `USP_Public_SaveOrder`(IN pint_Flag INT,INOUT pint_ID BIGINT,IN pint_Status INT,IN pint_CustomerID BIGINT,IN pint_ShippingAddressID BIGINT,IN pint_BillingAddresID BIGINT, IN pdec_ShippingCharge DECIMAL(18,2),IN pdec_Tax DECIMAL(18,2),IN pdec_Total DECIMAL(18,2),IN pdte_TransactionDate DATETIME)
BEGIN
IF(pint_Flag=0) THEN
    INSERT INTO orderheader(Status,
                                            CustomerID,
                                            ShippingAddressID,
                                            BillingAddressID,
                                            ShippingCharge,
                                            Tax,
                                            Total,
                                            TransactionDate)
                            VALUES (pint_Status,
                                            pint_CustomerID,
                                            pint_ShippingAddressID,
                                            pint_BillingAddresID,
                                            pdec_ShippingCharge,
                                            pdec_Tax,
                                            pdec_Total,
                                            pdte_TransactionDate);
        SET pint_ID=LAST_INSERT_ID();

ELSEIF(pint_Flag=1) THEN
    UPDATE orderheader
    SET     Status = pint_Status,
                CustomerID = pint_CustomerID,
                ShippingAddressID = pint_ShippingAddressID,
                BillingAddressID = pint_BillingAddresID,
                ShippingCharge = pdec_ShippingCharge,
                Tax = pdec_Tax,
                Total = pdec_Total,
                TransactionDate = pdte_TransactionDate
    WHERE   ID=pint_ID;
  IF(pint_Status=2) THEN
    UPDATE product SET Quantity = A.remain
    FROM product PR
    INNER JOIN (SELECT PR.ID AS ProductID, (PR.Quantity - OD.Quantity) AS remain
              FROM product P
              INNER JOIN orderdetail OD ON OD.ProductID = P.ID AND OD.OrderID = pint_ID) A ON PR.ID = A.ProductID
  ELSE
    RETURN
  END IF;
END IF;
END $$

DELIMITER ;

按如下方式修改您的 UPDATE 块并尝试。

IF(pint_Status=2) THEN
    UPDATE product PR
    INNER JOIN (SELECT PR.ID AS ProductID, (PR.Quantity - OD.Quantity) AS remain
                FROM product P
                INNER JOIN orderdetail OD ON OD.ProductID = P.ID AND OD.OrderID = pint_ID
               ) A ON PR.ID = A.ProductID
    SET PR.Quantity = A.remain;
ELSE
    RETURN
END IF;

检查 UPDATE 语句的语法:14.2.11 UPDATE Syntax.

.
.
.
UPDATE product SET Quantity = A.remain
FROM product PR
   INNER JOIN (SELECT PR.ID AS ProductID, (PR.Quantity - OD.Quantity) AS remain
               FROM product P
                 INNER JOIN orderdetail OD ON
                   OD.ProductID = P.ID AND
                   OD.OrderID = pint_ID
              ) A ON PR.ID = A.ProductID -- ; <- Add semicolon
.
.
.
/*ELSE
    RETURN*/

存储过程不需要RETURN

MySQL 的 UPDATE 语法不使用 FROM 关键字。如果要为要更新的 table 指定别名,只需将其放在 UPDATE tablename 子句之后即可。在您的子查询中,您应该使用别名 P,而不是 PR

UPDATE PRODUCT AS PR
INNER JOIN (SELECT P.ID AS ProductID, (P.Quantity - OD.Quantity) AS remain
          FROM product P
          INNER JOIN orderdetail OD ON OD.ProductID = P.ID AND OD.OrderID = pint_ID) A ON PR.ID = A.ProductID
SET PR.Quantity = A.remain

其实不用子查询,直接用PRODUCTorderdetail连接即可。

UPDATE PRODUCT AS PR
INNER JOIN orderdetail AS OD ON OD.ProductID = PR.ID
SET PR.Quantity = PR.Quantity - OD.Quantity
WHERE OD.OrderID = pintID