0

我正在尝试构建下一个存储过程,但它不起作用。

PREPARE STMT FROM 'SELECT a, b, c, d, e FROM TABLE_1 WHERE (`a` like `%?%` OR `b` like `%?%` OR `c` like `%?%` OR `d` like `%?%` OR `e` like `%?%`) LIMIT ?, ?';
EXECUTE STMT USING @search, @skip, @rows;

基本上,我试图在其中一列中搜索一个字符串,并返回适合的列的行。请帮我修复它,因为我收到下一个错误:

Unknown column '%?%' in 'where clause'
4

3 回答 3

0

这是因为数? 应该等于USING子句后的参数数量。你也给出了错误的报价?而不是"?"尝试:

PREPARE STMT FROM 'SELECT a, b, c, d, e FROM TABLE_1 WHERE (`a` like "%?%" OR `b` like "%?%" OR `c` like "%?%" OR `d` like "%?%" OR `e` like "%?%") LIMIT ?, ?';

EXECUTE STMT USING @search, @search, @search, @search, @search, @skip, @rows;

或尝试

SET @query1 = CONCAT('
    SELECT SQL_CALC_FOUND_ROWS a, b, c, d, e
    FROM TABLE_1
    WHERE (`a` like "%',@search,'%" OR
        `b` like "%',@search,'%" OR
        `c` like "%',@search,'%" OR
        `d` like "%',@search,'%" OR
        `e` like "%',@search,'%")
    LIMIT ',@skip,', ',@rows,'');

    PREPARE stmt FROM @query1; 
    EXECUTE stmt; 
    SELECT FOUND_ROWS() AS total_rows;
    DEALLOCATE PREPARE stmt;
于 2012-07-24T13:26:34.723 回答
0

mysql 上的给定示例似乎表明问号的数量应该与参数的数量相似?

http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

我错了吗?

在您的情况下,这意味着七个问号和七个参数。

一切顺利

于 2012-07-24T13:27:05.133 回答
0

尝试这个...

PREPARE STMT FROM 'SELECT a, b, c, d, e FROM TABLE_1 WHERE (`a` like "%?%" OR `b` like "%?%" OR `c` like "%?%" OR `d` like "%?%" OR `e` like "%?%") LIMIT ?, ?';

EXECUTE STMT USING @search, @search, @search, @search, @search, @skip, @rows;

笔记

变量的数量应该等于我们拥有的问号的数量。

你有 7 个问号,只有 3 个变量。所以错误来了 4th ?

于 2012-07-24T13:49:11.990 回答