2013-03-27 62 views
0

我寫過一個存儲過程。 shipmentnumbers和cartonid都應返回逗號分隔值。出貨編號即將到來,即逗號分隔值,但cartonid總是隻返回一個值,即使有多行。我如何解決這個問題。存儲過程中無法識別的GROUP_CONCAT值

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 ; 

回答

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; 
相關問題