如何在MySQL存储过程中传递字符串列表?

时间:2021-05-14 08:52:06

I'm trying to pass an array as a String in MySQL Stored Procedure but it doesn't work fine.

我在MySQL存储过程中尝试将数组作为字符串传递,但它不能正常工作。

Here's my SQL Codes:

这是我的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);

Any ideas?

什么好主意吗?

3 个解决方案

#1


5  

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存储过程,它使用逗号分隔的字符串作为变量输入

MySQL documention for FIND_IN_SET is here http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

FIND_IN_SET的MySQL文档在这里http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

So your procedure will become

所以你的程序会变成

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. fixed a typo, in your arguments you had typed categoy)

(p.s.修正了一个错别字,在你的论点中你键入了categoy)

#2


2  

you have to create a dynamic statment:

你必须创造一个动态的状态:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `search_equipment`(IN equip VARCHAR(100), IN category VARCHAR(255))
BEGIN
    SET @s = 
        CONCAT('SELECT *
            FROM Equipment
            WHERE e_description
            LIKE \'%',equip,'%\'
            AND e_type IN (',category,')');
    PREPARE stmt from @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt3;
END$$

#3


0  

This helps for me to do IN condition Hope this will help you..

这对我有帮助,但我希望这对你有帮助。

CREATE  PROCEDURE `test`(IN Array_String VARCHAR(100))
BEGIN
    SELECT * FROM Table_Name
    WHERE FIND_IN_SET(field_name_to_search, Array_String);

END//;

Calling:

调用:

call test('3,2,1');

#1


5  

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存储过程,它使用逗号分隔的字符串作为变量输入

MySQL documention for FIND_IN_SET is here http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

FIND_IN_SET的MySQL文档在这里http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

So your procedure will become

所以你的程序会变成

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. fixed a typo, in your arguments you had typed categoy)

(p.s.修正了一个错别字,在你的论点中你键入了categoy)

#2


2  

you have to create a dynamic statment:

你必须创造一个动态的状态:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `search_equipment`(IN equip VARCHAR(100), IN category VARCHAR(255))
BEGIN
    SET @s = 
        CONCAT('SELECT *
            FROM Equipment
            WHERE e_description
            LIKE \'%',equip,'%\'
            AND e_type IN (',category,')');
    PREPARE stmt from @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt3;
END$$

#3


0  

This helps for me to do IN condition Hope this will help you..

这对我有帮助,但我希望这对你有帮助。

CREATE  PROCEDURE `test`(IN Array_String VARCHAR(100))
BEGIN
    SELECT * FROM Table_Name
    WHERE FIND_IN_SET(field_name_to_search, Array_String);

END//;

Calling:

调用:

call test('3,2,1');