Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何将变量传递给IN子句?_Mysql_Variables_Stored Procedures_In Clause - Fatal编程技术网

Mysql 如何将变量传递给IN子句?

Mysql 如何将变量传递给IN子句?,mysql,variables,stored-procedures,in-clause,Mysql,Variables,Stored Procedures,In Clause,假设我有一个SP,它有一个SELECT语句,如下所示 SELECT product_id, product_price FROM product WHERE product_type IN ('AA','BB','CC'); 但数据必须通过包含值字符串的单个变量传递到子句中的。下面是链接 SELECT product_id, product_price FROM product WHERE product_type IN (input_variables); 但它不是这样工作的。你知道怎

假设我有一个SP,它有一个
SELECT
语句,如下所示

SELECT product_id, product_price FROM product 
WHERE product_type IN ('AA','BB','CC');
但数据必须通过包含值字符串的单个变量传递到子句中的
。下面是链接

SELECT product_id, product_price FROM product 
WHERE product_type IN (input_variables);

但它不是这样工作的。你知道怎么做吗?

创建一个用户定义的函数,将逗号分隔的值转换成表,通过将这两个值连接起来可以得到所需的结果


尝试使用准备好的语句:

像这样传递参数值-
'AA,BB,CC'
。然后,使用函数就足够了-

SELECT product_id, product_price
FROM product
WHERE FIND_IN_SET(product_type, param);
使用变量传递字符串是一个问题假设此解决方案 从其他查询窗口调用过程 输出
输入变量
来自哪里
php?
它实际上是从cron作业直接执行的MYSQL,因此它是SP的一个输入参数。用户输入这些参数取决于他们需要什么数据。所以你是说它是单个变量中的逗号分隔值?应该是。但是我可以按照我想要的方式来做(如果我找到了正确的方法:),但是这将是一个用户输入。单变量即使是逗号分隔的值也将被解释为单值。例如,变量
x='1'、'2'、'3'、'4'
。当您像这样将它传递给
中的
时,
其中y在(x)
中,它将被解释为
其中y在('1,2,3,4')
而不是
其中y在('1','2','3','4')
这将导致查询在没有索引的所有表上运行。我甚至认为这是一个有效的答案
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `spTestListValues`(_list varchar(200))
BEGIN
        SET @LIST=_list; -- assume this a paramter from the stored procedure
    SELECT NULL AS Id,  '' AS Description --insert null value to be used for list box population
    UNION  
    (SELECT id, Description
    FROM test_table
    WHERE FIND_IN_SET(id,@LIST) ORDER BY Description ASC) ;

END
call `spTestListValues`('4,5,3'); --no paramter currently for test
ID Description
NUll 
1   TEST1 
4   TEST2
5   TEST3