mysql在函数中追加带整数返回0的char

时间:2015-10-15 11:50:07

标签: mysql sql function

我在mysql中创建了这个函数,我希望结果具有(1,0,-2,-1)这样的定位值,而是给出结果0

DROP Function IF EXISTS get_preorder_status;
DELIMITER $$
CREATE Function get_preorder_status(loc_pre_order_id int) RETURNS INT(10)
BEGIN     
DECLARE all_status CHAR(10) DEFAULT '';
DECLARE temp_status INT(2);
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;

SELECT  count(*) into n
FROM pre_order_detail
WHERE pre_order_id = loc_pre_order_id;

WHILE i<n DO 
SELECT  customer_response_status into temp_status
FROM pre_order_detail
WHERE pre_order_id = loc_pre_order_id LIMIT i,1;
SET all_status = CONCAT(all_status,', ',CAST(temp_status AS CHAR));
SET i = i + 1;
END WHILE;`

RETURN all_status;
END$$
DELIMITER ;

all_status是char类型变量,但temp_status是INT,我希望最终结果在一个字符串类型值中使用逗号saparated concat那些整数值

请帮忙

1 个答案:

答案 0 :(得分:0)

您的函数正在返回int。尝试让它返回一个字符类型。

换句话说,返回时字符串将转换为int。

逻辑可以更简单地写成:

SELECT group_concat(customer_response_status) into temp_status
FROM pre_order_detail
WHERE pre_order_id = loc_pre_order_id ;

无需循环。