我写了一个存储过程。 shippingnumbers和cartonid都应返回逗号分隔值。出货数量正常,即逗号分隔值,但即使有多行,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 ;
答案 0 :(得分: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;