使用MYSQL中的存储过程计算成本

时间:2014-05-22 02:13:51

标签: mysql loops stored-procedures syntax

我的数据库中有两个表:Package and Shipment。货件中可以有多个包裹。 (因此每个包都有一个唯一的shipment_track_no)

Package(package_number, type, content, weight, shipment_track_no)

Shipment(tracking_number, sender_id, receiver_name, receiver_phone_number, method, is_delivered, shipment_date)

我想在MYSQL中创建一个存储过程,用于计算货件的成本,迭代货件中的包裹。

计算有几个步骤:

  • 首先,根据类型(1-5枚枚)来评估包装。
  • 如果包具有特殊内容(1-3枚举),则添加额外内容。
  • 在计算每个发货包的成本后,我将其添加到a 共同的总和。根据发货的送货方式额外 也可以加上总和。


我使用我能找到的所有可用资源提出的代码如下。我想我搞乱了语法,但主流应该是正确的。如果有人用正确的语法帮助我,我将非常感激。

DROP PROCEDURE IF EXISTS cost_of_shipment;

DELIMITER $$
CREATE PROCEDURE cost_of_shipment(IN shipment_id_fed int(11),
                                    OUT total decimal(12,2))  
BEGIN 
DROP TEMPORARY TABLE IF EXISTS temporaryTable;
CREATE TEMPORARY TABLE temporaryTable engine=memory SELECT package_number, Package.`type`, content, weight FROM Package WHERE shipment_track_no = shipment_id_fed;

DECLARE i INT(11) DEFAULT 1;

DECLARE rowCount INT(11) DEFAULT 0;
SET rowCount = (SELECT COUNT(package_number) FROM temporaryTable); 



DECLARE shipment_cost DECIMAL (12,2) DEFAULT = 0.00;


WHILE (i <= rowCount)
BEGIN

        DECLARE ipackage_number INT(11), ipackage_type INT(11), icontent INT(11), iweight DECIMAL(12,2), package_cost DECIMAL(12,2);

        SELECT ipackage_number = package_number, ipackage_type = package_type, icontent = content , iweight = weight FROM temporaryTable LIMIT 1;
        IF ipackage_type = 1 OR ipackage_type = 2 OR  ipackage_type = 3
        THEN
        SET package_cost = 10;
        END IF;

        IF ipackage_type = 4
        THEN
        SET package_cost = 15;
        END IF;

        IF ipackage_type = 5 AND iweight<20
        THEN
        SET package_cost = 20;
        END IF;

        IF ipackage_type = 5 AND iweight>20
        THEN
        SET package_cost = 20 + (iweight-20)*1;
        END IF;

        IF icontent = 2
        THEN
        SET package_cost = package_cost + 10;
        END IF;

        IF icontent = 3
        THEN
        SET package_cost = package_cost * 1.1;
        END IF;

        shipment_cost = shipment_cost + package_cost; 

        DELETE FROM temporaryTable WHERE package_number = ipackage_number;
        SET i = i  + 1
END

        IF (SELECT method FROM Shipment WHERE tracking_number = shipment_id_fed) = 2
        THEN
        SET shipment_cost = shipment_cost +10; 
        END IF;

        IF (SELECT method FROM Shipment WHERE tracking_number = shipment_id_fed) = 3
        THEN
        SET shipment_cost = shipment_cost +25; 
        END IF;

    SELECT shipment_cost INTO total FROM Shipment;
END $$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

我为包装和装运重新设计了表格;为他们增加了成本属性。我处理了创建插入和更新包的触发器的问题。

在插入触发器上计算包的费用

    DELIMITER $$
    CREATE TRIGGER package_cost_insert BEFORE INSERT ON Package
    FOR EACH ROW
    BEGIN
        IF new.type = 1 OR new.type = 2 OR new.type = 3  THEN
             SET new.cost = new.cost + 10.00;
        END IF;

        IF new.type = 4  THEN
             SET new.cost = new.cost + 15.00;
        END IF;

        IF new.type = 5 AND new.weight< 20.00 THEN
             SET new.cost = new.cost + 20.00;
        END IF;

        IF new.type = 5 AND new.weight> 20.00  THEN
             SET new.cost = new.cost + 20.00 + ROUND((new.weight -20.00),0);
        END IF;

        IF new.content = 1 THEN
             SET new.cost = new.cost + 0.00;
        END IF;

        IF new.content = 2  THEN
             SET new.cost = new.cost + 10.00;
        END IF;

        IF new.content = 3  THEN
             SET new.cost = new.cost *1.1;
        END IF;
    END;
    $$

    DELIMITER ;

更新触发器,用于计算包的费用

    DELIMITER $$
    CREATE TRIGGER package_cost_update BEFORE UPDATE ON Package
    FOR EACH ROW
    BEGIN
        SET new.cost = 0;
        IF new.type = 1 OR new.type = 2 OR new.type = 3  THEN
             SET new.cost = new.cost + 10.00;
        END IF;

        IF new.type = 4  THEN
             SET new.cost = new.cost + 15.00;
        END IF;

        IF new.type = 5 AND new.weight< 20.00 THEN
             SET new.cost = new.cost + 20.00;
        END IF;

        IF new.type = 5 AND new.weight> 20  THEN
             SET new.cost = new.cost + 20.00 + ROUND((new.weight -20.00),0);
        END IF;

        IF new.content = 1 THEN
             SET new.cost = new.cost + 0.00;
        END IF;

        IF new.content = 2  THEN
             SET new.cost = new.cost + 10.00;
        END IF;

        IF new.content = 3  THEN
             SET new.cost = new.cost *1.1;
        END IF;
    END;
    $$

    DELIMITER ;

在插入触发器上计算出货成本

DELIMITER $$
CREATE TRIGGER shipment_cost_insert AFTER INSERT ON Package
FOR EACH ROW
BEGIN
    UPDATE Shipment SET Shipment.`shipment_cost` = (SELECT SUM(cost) FROM Package WHERE Package.`shipment_track_no` = Shipment.`tracking_number`);
        IF (SELECT method FROM Shipment WHERE new.`shipment_track_no` = Shipment.`tracking_number`) = 1 THEN
        UPDATE Shipment SET Shipment.`shipment_cost` = Shipment.`shipment_cost` + 0.00;
    END IF;

    IF (SELECT method FROM Shipment WHERE new.`shipment_track_no` = Shipment.`tracking_number`) = 2 THEN
        UPDATE Shipment SET Shipment.`shipment_cost` = Shipment.`shipment_cost` + 10.00;
    END IF;

    IF (SELECT method FROM Shipment WHERE new.`shipment_track_no` = Shipment.`tracking_number`) = 3 THEN
        UPDATE Shipment SET Shipment.`shipment_cost` = Shipment.`shipment_cost` + 25.00;
    END IF;
END;
$$

DELIMITER ;

更新触发器,用于计算货件的成本

DELIMITER $$
CREATE TRIGGER shipment_cost_update AFTER UPDATE ON Package
FOR EACH ROW
BEGIN
    UPDATE Shipment SET Shipment.`shipment_cost` = (SELECT SUM(cost) FROM Package WHERE Package.`shipment_track_no` = Shipment.`tracking_number`);

    IF (SELECT method FROM Shipment WHERE new.`shipment_track_no` = Shipment.`tracking_number`) = 1 THEN
        UPDATE Shipment SET Shipment.`shipment_cost` = Shipment.`shipment_cost` + 0.00;
    END IF;

    IF (SELECT method FROM Shipment WHERE new.`shipment_track_no` = Shipment.`tracking_number`) = 2 THEN
        UPDATE Shipment SET Shipment.`shipment_cost` = Shipment.`shipment_cost` + 10.00;
    END IF;

    IF (SELECT method FROM Shipment WHERE new.`shipment_track_no` = Shipment.`tracking_number`) = 3 THEN
        UPDATE Shipment SET Shipment.`shipment_cost` = Shipment.`shipment_cost` + 25.00;
    END IF;

END;
$$

DELIMITER ;