如何比较两个表SUM和使用MySql触发器更新另一个表

时间:2018-12-31 09:38:19

标签: mysql triggers

  

比较两个表SUM并使用MySql触发器更新另一个表

DELIMITER $$  
    CREATE TRIGGER change_com_status_on_sales_table AFTER INSERT ON `commision_calculation` // COMMISSION TABLE 
    FOR EACH ROW 
    BEGIN  
        DECLARE totalQnt1, totalQnt2 DOUBLE;
        DECLARE sales_id INT;

    SET @sales_id=(SELECT sales_info_id
                      FROM commercial_invoice WHERE ci_no = NEW.ci_no); 

    SET @totalQnt1=(SELECT sum(cc.quantity) as total_qnt1 
                    FROM commision_calculation cc
                    LEFT JOIN commercial_invoice ci ON cc.ci_id = ci.id         
                    WHERE cc.comission_type = 'Remex' AND ci.sales_info_id= @sales_id
            GROUP BY @sales_id);

    SET @totalQnt2=(SELECT SUM(sii.quantity) as total_income
                      FROM sales_info_item sii WHERE sii.sales_info_id = @sales_id
                      GROUP BY @sales_id);                
    IF (@totalQnt2 > @totalQnt1) THEN
    UPDATE sales_information 
    SET ci_status = 'Partial'
    WHERE id = @sales_id;
    END IF;
    IF (@totalQnt2 = @totalQnt1) THEN
    UPDATE sales_information 
    SET ci_status = 'Full'
    WHERE id = @sales_id;
    END IF; 
    END; $$  
    DELIMITER ;

Jahid Hossain提到的上述错误

我收到“ SQLSTATE [21000]:违反基数:1242子查询返回多于1行的消息”

0 个答案:

没有答案