0

我写了一个存储过程。shippingnumbers 和 cartonid 都应返回逗号分隔值。shippingnumbers 正确出现,即逗号分隔值,但 cartonid 始终只返回一个值,即使有多行也是如此。我该如何解决这个问题。

 DELIMITER $$
    CREATE PROCEDURE `sp_deleteOrderData`(orderid BIGINT(11))
    BEGIN
        DECLARE shipmentnumbers VARCHAR(1000);
        DECLARE cartonid VARCHAR(1000);

        SELECT GROUP_CONCAT(a_shipmentid) FROM t_shipment WHERE a_orderid = orderid INTO shipmentnumbers;

        SELECT GROUP_CONCAT(a_cartonid) FROM t_carton WHERE a_shipmentid IN (shipmentnumbers) INTO cartonid;

    SELECT shipmentnumbers;
    /*SELECT  cartonid;  */
    END$$

    DELIMITER ;
4

1 回答 1

1

为什么不尝试以下方法,使用内部连接来匹配所有组合

DELIMITER $$
CREATE PROCEDURE `sp_deleteOrderData`(orderid BIGINT(11))
BEGIN
    DECLARE shipmentnumbers VARCHAR(1000);
    DECLARE cartonid VARCHAR(1000);

    SELECT
        GROUP_CONCAT(a_shipmentid),
        GROUP_CONCAT(a_cartonid)
    INTO
        shipmentnumbers,
        cartonid
    FROM t_shipment ts
    LEFT JOIN t_carton tc
    ON tc.a_shipmentid = ts.a_shipmentid

    WHERE ts.a_orderid = orderid;

    SELECT 
        shipmentnumbers,
        cartonid;
END$$

DELIMITER ;

如果您尝试从这些表中删除,尽管您可以通过一些连接更有效地执行此操作,即

DELETE ts,tc
FROM t_shipment ts
LEFT JOIN t_carton tc
ON tc.a_shipmentid = ts.a_shipmentid
WHERE ts.a_orderid = orderid;
于 2013-03-27T09:56:55.460 回答