0

我在 mysql 中有一个带有几个查询的存储过程,我需要对该查询执行一些操作。

这是存储过程中的一些代码:

    BEGIN
    SET @@session.collation_connection = @@global.collation_connection;

    DROP TEMPORARY TABLE IF EXISTS innerContainers;
    CREATE TEMPORARY TABLE `innerContainers` (
        `id_container` INT(10) NOT NULL,
        `display_name` VARCHAR(100) NOT NULL,
        PRIMARY KEY (`id_container`)
        )
        ENGINE = memory;

    INSERT INTO innerContainers(id_container, display_name)
    (SELECT c1.id_container, c1.display_name 
     FROM container_presentation cp
     LEFT JOIN presentation p USING(id_presentation)
     LEFT JOIN container c1 ON p.id_container = c1.id_container
     WHERE c1.isDeleted = 0 AND c1.isActive = 1 AND
             cp.id_container = in_id_container)
    UNION
    (SELECT c1.id_container, c1.display_name 
     FROM container_assembly_item cp
     LEFT JOIN presentation p USING(id_presentation)
     LEFT JOIN container c1 ON p.id_container = c1.id_container
     WHERE c1.isDeleted = 0 AND c1.isActive = 1 AND
           cp.id_container = in_id_container);

    SELECT mad.id_container,
             mat.sign_stock, 
             ma.id_management_start_point,  
             ma.id_management_end_point,
             mad.quantity
    FROM management_activity ma
    LEFT JOIN management_activity_type mat ON ma.id_management_activity_type = mat.id_management_activity_type
    LEFT JOIN management_activity_detail mad ON ma.id_management_activity = mad.id_management_activity
    LEFT JOIN management_stock_point msp ON ma.id_management_end_point = msp.id_management_stock_point
    LEFT JOIN management_stock_point msp1 ON ma.id_management_start_point = msp1.id_management_stock_point
    WHERE mad.id_container IN (SELECT id_container FROM innerContainers)
    ORDER BY mad.id_container ASC;
END

现在,在最后一个查询之后.. 我需要执行一些操作并根据第二个查询中的值为临时表中的每个 id_container 返回一个值。像这样的东西:

在第二个查询中 foreach id_container 我有一个 resultValue 我需要:

如果 sign_stock == 1 和其他一些条件,则 resultValue -= 数量,如果 sign_stock == 2 和其他一些条件,则 resultValue += 数量。遍历 id_container 行后的最终 resultValue 将是我想要的临时表中的 id_container 。我不知道如何进行该操作..有人可以帮助我吗?

4

1 回答 1

0

除非在过程调用之后需要数据,否则不要创建临时表。无论哪种方式,为了迭代SELECT查询的结果,请使用CURSOR.

链接的手册页中提供了一个简单的示例。

于 2013-08-13T15:28:42.283 回答