如何在MySQL存储过程中传递字符串列表? [英] How to Pass a List of String in MySQL Stored Procedure?
问题描述
我正在尝试在MySQL存储过程中将数组作为字符串传递,但无法正常工作.
I'm trying to pass an array as a String in MySQL Stored Procedure but it doesn't work fine.
这是我的SQL代码:
CREATE DEFINER=`root`@`localhost` PROCEDURE `search_equipment`(IN equip VARCHAR(100), IN category VARCHAR(255))
BEGIN
SELECT *
FROM Equipment
WHERE e_description
LIKE CONCAT("%",equip,"%")
AND e_type IN (category)
END
这是我如何调用该过程:
And here's how i call the procedure:
String type = "'I.T. Equipment','Office Supply'";
CALL search_equipment('some equipment', type);
有什么想法吗?
推荐答案
您的朋友在这里是FIND_IN_SET.我首先在此问题中遇到了该方法:也在此问题中涉及
Your friend here is FIND_IN_SET I expect. I first came across that method in this question : also covered in this question MYSQL - Stored Procedure Utilising Comma Separated String As Variable Input
有关FIND_IN_SET的MySQL文档,请参见 http://dev.mysql.com/doc/refman/5.0/zh-CN/string-functions.html#function_find-in-set
MySQL documention for FIND_IN_SET is here http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set
所以您的程序将变成
CREATE DEFINER=`root`@`localhost`
PROCEDURE `search_equipment`(
IN equip VARCHAR(100),
IN category VARCHAR(255)
)
BEGIN
SELECT *
FROM Equipment
WHERE e_description LIKE CONCAT("%",equip,"%")
AND FIND_IN_SET(e_type,category)
END
这取决于类别字符串是一个逗号分隔的列表,因此您的呼叫代码将变为
This relies on the category string being a comma-delimited list, and so your calling code becomes
String type = "I.T. Equipment,Office Supply";
CALL search_equipment('some equipment', type);
(p.s.修正了一个错字,在您输入参数时输入了类别)
(p.s. fixed a typo, in your arguments you had typed categoy)
这篇关于如何在MySQL存储过程中传递字符串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!