MySQL concat-在自定义函数内-不工作

MySQL concat-在自定义函数内-不工作,mysql,concat,stored-functions,Mysql,Concat,Stored Functions,我试着这么做 CREATE FUNCTION getOneCentOrderIds (s text) RETURNS text BEGIN DECLARE no_more_orders, ent_id INT default 0; DECLARE ids text; DECLARE orders_cur CURSOR FOR SELECT entity_id FROM sales_flat_order WHERE total_due = 0.01; DECLARE

我试着这么做

CREATE FUNCTION getOneCentOrderIds (s text) RETURNS text
BEGIN
    DECLARE no_more_orders, ent_id INT default 0;
    DECLARE ids text;
    DECLARE orders_cur CURSOR FOR SELECT entity_id FROM sales_flat_order WHERE total_due = 0.01;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_orders = 1;
    OPEN orders_cur;
        FETCH NEXT FROM orders_cur INTO ent_id;
        REPEAT
            SET ids = CONCAT(ids, ', ', ent_id);
            FETCH orders_cur INTO ent_id;
        UNTIL no_more_orders END REPEAT;
    CLOSE orders_cur;
    RETURN ids;
END$
但是当我执行这个函数时,我得到了null

如果我只是删除concat并保留
SET-id=ent\u-id
,我将获得游标中的最后一个id,正如预期的那样


我应该如何进行连接?

不需要创建函数,上面的操作可以在查询中完成,如下所示

SELECT group_concat(entity_id) FROM sales_flat_order WHERE total_due = 0.01;
如果concat()函数的任何参数为NULL,则该函数返回NULL。试一试

DECLARE ids text DEFAULT '';
这将确保对CONCAT的第一次调用没有空参数