0

I am facing an issue related to group_concat because when I am using this the total record can't fetch from the table based in the transactionID Here is my stored procedure code. In this I am using a split function called strSplit.

BEGIN

DECLARE iCount int;

DECLARE i int;

DECLARE txn VARCHAR(65000);

DECLARE txId VARCHAR(17);

CREATE TEMPORARY TABLE report_transaction (client_id INT NOT NULL AUTO_INCREMENT, productName VARCHAR(200), itxnId VARCHAR(100),PRIMARY KEY(client_id));

select count(distinct(tt.TxnId)) into iCount from tbl_transaction tt;

SELECT group_concat(distinct((tt.TxnId)) separator ', ') product into txn  from tbl_transaction tt;

SET i=1;

WHILE i<iCount+1 DO

select strSplit(txn, ',', i) into txId;

SELECT RTRIM(LTRIM(txId));

 INSERT INTO report_transaction(productName,itxnId) select group_concat((tt.ProductName) separator ',') products,tt.TxnId from tbl_transaction tt where tt.TxnId= txId;

    SET i = i + 1;

END WHILE;

     SELECT * FROM report_transaction;
     drop table report_transaction;

END

I want result like a table record but it shows only first record and then other fields are null.

4

1 回答 1

0

我认为你想要的可以通过一个查询来完成:

INSERT INTO report_transaction(productName,itxnId)
    select group_concat((tt.ProductName) separator ',') as products, tt.TxnId
    from tbl_transaction tt
    group by tt.TxnId;

您不需要临时表来存储 ID,或者修剪或拆分任何内容。

通常,在适当的时候使用 SQL 查询可以获得更好的性能。

顺便说一句,如果您确实需要遍历表中的行,那么您应该了解游标。

于 2012-10-05T15:40:44.677 回答