2013-05-31 27 views
0

我創建MySQL的功能如下的MySQL函數不給正確的結果

DELIMITER $$ 
DROP FUNCTION IF EXISTS `GetProductIDFunc`$$ 
CREATE FUNCTION `GetProductIDFunc`(countryid INT (10)) 
    RETURNS VARCHAR(200) 
BEGIN 
declare out_id VARCHAR;  

select country_percentage INTO out_id from country_markup where estatus = '1' AND `country_id` REGEXP '[[:<:]]countryid [[:>:]]' limit 1;  
RETURN out_id; 
END$$ 
DELIMITER ; 

我呼籲像如下 SELECT GetProductIDFunc(223) 此功能,但它給了我NULL值,而不是7,是我的預期結果值。這裏 檢查樣本數據,上述結果[鏈接] http://sqlfiddle.com/#!2/6aa92

注:如果我用靜態值替換'[[:<:]]countryid [[:>:]]''[[:<:]]223 [[:>:]]'比函數返回的願望的結果。 幫助將不勝感激。

回答

2

MySQL不會替換字符串內的變量值。您可以使用concat形成正則表達式,例如:

select country_percentage INTO out_id from country_markup 
where estatus = '1' AND `country_id` REGEXP concat('[[:<:]]',countryid,'[[:>:]]') 
limit 1; 
+0

感謝Ton Friend.i承認您的義務。 –