【问题标题】:How Use Count and Like Clause Together in MySql Stored Procedure?MySql存储过程中如何同时使用count和like子句?
【发布时间】:2014-01-10 09:19:09
【问题描述】:

我在下面使用了动态获取表名并获取搜索关键字的存储过程。

 DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetTableLength`(IN TABLE_NAME VARCHAR(100),
                                                                           IN SEARCH VARCHAR(150)) BEGIN IF (SEARCH = '') THEN
SET @query = CONCAT('select count(*) as total from ',TABLE_NAME); ELSE
SET @query = CONCAT('select count(userId) as total from ',TABLE_NAME, ' WHERE userName like ', SEARCH); END IF; PREPARE stmt3
FROM @query; EXECUTE stm3; END

当我像下面这样调用上述程序时。

   CALL GetTableLength('usertbl', 'kisan')

这里,kisan 是一个搜索关键字。

然后出现以下错误。怎么了?

错误代码:1054。“where 子句”中的未知列“kisan”

【问题讨论】:

    标签: mysql stored-procedures


    【解决方案1】:

    尝试如下使用:

    set @query = CONCAT("select count(userId) as total from ",table_name, " WHERE userName like '", search,"'");
    

    你也可以试试:

    set @query = CONCAT("select count(userId) as total from ",table_name, " WHERE userName like '%", search,"%'");
    

    【讨论】:

      【解决方案2】:

      您需要在like 语句中使用引用'' 传递搜索参数

      【讨论】:

        猜你喜欢
        • 2012-01-20
        • 2018-12-24
        • 2011-09-10
        • 2011-02-08
        • 2013-07-12
        • 2013-07-16
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        相关资源
        最近更新 更多