在我的代码中,我将一个字符串变量(' 4,5,6')传递给我的存储过程,该存储过程从该字符串(4,5,6-Name)中展开。我在存储过程中使用IN子句,我知道IN子句接受(' 4',' 5',' 6')或简单地(4,5, 6)这种类型的数据,但我无法在我的存储过程中使用它。如果有任何其他方法可以帮助我。
CREATE DEFINER = 'root'@'localhost'
PROCEDURE kbm.report_site_transfer(IN siteid VARCHAR(50), IN datefrom VARCHAR (15), IN dateto VARCHAR (15))
SELECT
*
FROM
tbl_transfer
WHERE
tbl_transfer.siteNameID IN (siteid) AND (tbl_transfer.paymentDate BETWEEN datefrom AND dateto);
答案 0 :(得分:0)
您是否曾尝试过类似的内容,因为我为您提供了链接 - 您将整个查询创建为字符串&然后作为准备好的声明执行:
CREATE DEFINER = 'root'@'localhost'
PROCEDURE kbm.report_site_transfer(IN siteid VARCHAR(50), IN datefrom VARCHAR (15), IN dateto VARCHAR (15))
Set @query = CONCAT("SELECT * FROM tbl_transfer WHERE tbl_transfer.siteNameID IN (", siteid, ") AND (tbl_transfer.paymentDate BETWEEN ", datefrom, " AND ", dateto, ")";
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
如果你想要单引号版本,你可以使用它:
Set @query = CONCAT("SELECT * FROM tbl_transfer WHERE tbl_transfer.siteNameID IN ('", REPLACE(siteid,",", "','"), "') AND (tbl_transfer.paymentDate BETWEEN ", datefrom, " AND ", dateto, ")";
答案 1 :(得分:0)
此代码也正常工作。我使用了FIND_IN_SET函数:
FIND_IN_SET(tbl_transfer.siteNameID, siteid)
完整代码:
CREATE DEFINER = 'root'@'localhost'
PROCEDURE kbm.report_site_transfer(IN siteid VARCHAR(50), IN datefrom VARCHAR (15), IN dateto VARCHAR (15))
SELECT
*
FROM
tbl_transfer
WHERE
FIND_IN_SET(tbl_transfer.siteNameID, siteid) AND (tbl_transfer.paymentDate BETWEEN datefrom AND dateto);