0

当我尝试使用“search_string”作为存储过程的参数运行此查询时,我的 MySQL 数据库在“字段列表”错误消息中为我提供了“未知列”“search_string” :

DROP PROCEDURE IF EXISTS select_title_from_book_table_like;
CREATE PROCEDURE select_title_from_book_table_like(IN IN_TITLE VARCHAR(255))
BEGIN
    PREPARE stmt FROM
    "SELECT   b.book_id,
              LEFT(b.title, 36) AS title,
              b.edition,
              GROUP_CONCAT(CONCAT_WS(' ', a.last_name,
                                          a.first_name,
                                          a.patronymic)
                                 ORDER BY a.last_name
                                 SEPARATOR ', ') AS authors,
              YEAR(b.published) AS published,
              LEFT(b.pages, 8)  AS pages,
              b.isbn,
              s.subject,
              l.language,
              p.publisher
     FROM    (SELECT * FROM book WHERE title LIKE '%?%') AS b
              LEFT JOIN book_author AS ba
                     ON b.book_id = ba.book_id
              LEFT JOIN author AS a
                     ON ba.author_id = a.author_id
              LEFT JOIN language AS l
                     ON b.language_id = l.language_id
              LEFT JOIN subject AS s
                     ON b.subject_id = s.subject_id
              LEFT JOIN publisher AS p
                     ON b.publisher_id = p.publisher_id
     GROUP BY b.book_id
     ORDER BY b.title ASC";

    SET @param1 = IN_TITLE; 

    EXECUTE stmt USING @param1;
    DEALLOCATE PREPARE stmt;
END$$

这是调用该过程的代码:

ShowStringGrid(StringGrid,
    'CALL select_title_from_book_table_like(' + Trim(SearchBox.Text) + ')');
4

1 回答 1

0

我认为你准备好的陈述在这里出错了:

WHERE    b.title LIKE '%?%'

您不想在%那里设置,因为现在您的问号在'. 更好的选择是使用

WHERE    b.title LIKE ?

然后将 % 添加到您的实际参数中。

对这个问题的答案的评论中,您还可以发现您可能会在查询中这样做,但我没有这方面的经验:

WHERE    b.title LIKE '%' || ? || '%'
于 2013-04-25T07:57:46.727 回答