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

Tha*_*anu 33 mysql variables stored-procedures in-clause

假设我有一个SP,其SELECT声明如下,

SELECT product_id, product_price FROM product 
WHERE product_type IN ('AA','BB','CC');
Run Code Online (Sandbox Code Playgroud)

但是数据转到该IN子句必须通过包含值字符串的单个变量.下面有东西链接

SELECT product_id, product_price FROM product 
WHERE product_type IN (input_variables);
Run Code Online (Sandbox Code Playgroud)

但它没有那样工作.知道怎么做吗?

Dev*_*art 51

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

SELECT product_id, product_price
FROM product
WHERE FIND_IN_SET(product_type, param);
Run Code Online (Sandbox Code Playgroud)

  • 这将导致查询在没有索引的所有表上运行.即使这是一个有效的答案,也不稳健 (7认同)