Question

I have a table like this below. and table name is sample.

id | product
--------------
1   | pen
1   | book
1   | eraser
2   | mouse
2   | keyboard

I need an output like this by using procedure. thanks advance

id | product
-------------
1  | pen,book,eraser
2  | mouse,keyboard
Was it helpful?

Solution 2

Here you go try this procedure :

DELIMITER //

CREATE PROCEDURE ProcedureName()

BEGIN

SELECT id,GROUP_CONCAT(product) FROM sample group by id;

END //

DELIMITER ;

It is not well tested I believe you can debug it. For reference you can go through the following tutorial it will give you basic idea : Mysql Store Procedure Tutorial

OTHER TIPS

Writing procedures are as simple as writing SQL queries. For your requirement, a tested procedure is:

    DELIMITER $$

    CREATE PROCEDURE `getConcatedProduct` ()
    BEGIN
       -- this is a comment
       SELECT id,GROUP_CONCAT(product) FROM sample group by id;
    END
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top