0

我是 ruby​​ on rails 的新手,我有搜索文本框,然后每次我输入一个撇号 (') eg testing' 单词.....我总是收到错误:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's  word%' OR english_name LIKE '%testing's  word%' OR chinese_name LIKE '%testin' at line 1: SELECT COUNT(DISTINCT `jos_store`.`id`) FROM `jos_store` LEFT OUTER JOIN (SELECT id as store_replenishment, store, MAX(stock_movement) AS stock_movement FROM jos_store_replenishment GROUP BY store) AS replenishment ON replenishment.store = jos_store.id LEFT OUTER JOIN jos_stock_movement ON jos_stock_movement.id = replenishment.stock_movement WHERE (store_id LIKE '%testing's  word%' OR english_name LIKE '%testing's  word%' OR chinese_name LIKE '%testing's  word%')

你能帮我解决我的问题吗?

4

1 回答 1

1

尝试转义撇号,例如 english_name LIKE '%testing\'s word%'

如果您正在使用 '%PHRASE%' 并且 %PHRASE% 内还有另一个 ',它认为您已经结束了 like 子句并给您一个错误 - 如果您使用转义字符转义,例如 \',它应该可以工作:因此,使用您的错误短语,例如:

<snip /> WHERE (store_id LIKE '%testing\'s  word%' OR english_name LIKE '%testing\'s  word%' OR chinese_name LIKE '%testing\'s  word%')    

请注意,我使用了 '%testing\'s word%' 来确保它不会认为第二个撇号结束 LIKE 子句

于 2012-07-13T08:48:37.047 回答