喜欢不在存储过程中工作

时间:2013-08-07 07:54:47

标签: php mysql database stored-procedures

我使用like运算符编写了存储过程,但是我收到了mysql的错误。 这是我的疑问:

CREATE DEFINER=`ecp`@`localhost` PROCEDURE `sp_getTier4CatBanner`(pSiteid int,pTier4 varchar(100))
BEGIN
    SET @SS = CONCAT("SELECT *  FROM `categorybanners` WHERE TIERCODES LIKE %",pTier4,"%"," and SITEID=",pSiteid," and CATLEVEL='tire4'");
    PREPARE STMT FROM @SS;
END

当我打印查询时,我得到以下结果:

SELECT *  FROM `categorybanners` WHERE TIERCODES LIKE %t2_05% and SITEID=46 and  CATLEVEL='tire4'

错误的主要原因遗漏了%t2_05%的单引号。 请让我知道如何使用单引号,使查询看起来像这样:

SELECT *  FROM `categorybanners` WHERE TIERCODES LIKE '%t2_05%' and SITEID=46 and  CATLEVEL='tire4'

1 个答案:

答案 0 :(得分:1)

只需将单引号放在第一个%前面,然后放在最后一个%:

之后
SET @SS = CONCAT("SELECT *  FROM `categorybanners` WHERE TIERCODES LIKE '%",pTier4,"%'"," and SITEID=",pSiteid," and CATLEVEL='tire4'");

这样你的陈述就变成了:

SELECT * FROM categorybanners WHERE TIERCODES LIKE '%t2_05%' and SITEID=46 and CATLEVEL='tire4'