如何在SQL LIKE中使用'\'... ESCAPE'\'

时间:2019-11-30 16:10:10

标签: php mysql escaping sql-like

我正在尝试删除描述中未包含快速聊天用户名的所有缓存项。在尝试转义破折号时,我一直遇到语法问题吗?

查询:

$cacheItems = CacheItem::whereRaw("description NOT LIKE '%'")
      ->whereRaw("description NOT LIKE '%?%'")
      ->whereRaw("description NOT LIKE '%?? %'")
      ->whereRaw("description NOT LIKE '%?? :%'")
      ->whereRaw("description NOT LIKE '%????????%'")
      ->whereRaw("description NOT LIKE '%sc\'%'")
      ->whereRaw("description NOT LIKE '%sc\_%' ESCAPE '\'")
      ->whereRaw("description NOT LIKE '%sc \- %' ESCAPE '\'")
      ->whereRaw("description NOT LIKE '%sc %'")
      ->whereRaw("description NOT LIKE '%sc:%'")
      ->whereRaw("description NOT LIKE '%scm %'")
      ->whereRaw("description NOT LIKE '%scm;%'")
      ->whereRaw("description NOT LIKE '%sc- %'")
      ->whereRaw("description NOT LIKE '%sc.%'")
      ->whereRaw("description NOT LIKE '%sc-%'")
      ->whereRaw("description NOT LIKE '%SC -  %'")
      ->whereRaw("description NOT LIKE '%Sc ~%'")
      ->whereRaw("description NOT LIKE '%sc ~%'")
      ->whereRaw("description NOT LIKE '%sc;%'")
      ->whereRaw("description NOT LIKE '%sc~%'")
      ->whereRaw("description NOT LIKE '%sc/%'")
      ->whereRaw("description NOT LIKE '%snapchat:%'")
      ->whereRaw("description NOT LIKE '%snapchat - %'")
      ->whereRaw("description NOT LIKE '%snapchat-%'")
      ->whereRaw("description NOT LIKE '%snap~%'")
      ->whereRaw("description NOT LIKE '%snap =%'")
      ->whereRaw("description NOT LIKE '%snap•%'")
      ->whereRaw("description NOT LIKE '%snap%'")
      ->whereRaw("description NOT LIKE '%snap~%'")
      ->whereRaw("description NOT LIKE '%snapchat%'")
      ->whereRaw("description NOT LIKE '%snapchat ~ %'")
      ->whereRaw("description NOT LIKE '%snap:%'")
      ->whereRaw("description NOT LIKE '%add my sc%'")
      ->whereRaw("description NOT LIKE '%add me on sc%'")
      ->whereRaw("description NOT LIKE '%add me sc%'")
      ->whereRaw("description NOT LIKE 'snap %'")
->get();

错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 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 '%sc \- %' ESCAPE '\' and description NOT LIKE '%sc %' and description NOT LIKE '' at line 1 (SQL: select * from `cache_items` where description NOT LIKE '%' and description NOT LIKE '%?%' and description NOT LIKE '%?? %' and description NOT LIKE '%?? :%' and description NOT LIKE '%????????%' and description NOT LIKE '%sc\'%' and description NOT LIKE '%sc\_%' ESCAPE '\' and description NOT LIKE '%sc \- %' ESCAPE '\' and description NOT LIKE '%sc %' and description NOT LIKE '%sc:%' and description NOT LIKE '%scm %' and description NOT LIKE '%scm;%' and description NOT LIKE '%sc- %' and description NOT LIKE '%sc.%' and description NOT LIKE '%sc-%' and description NOT LIKE '%SC -  %' and description NOT LIKE '%Sc ~%' and description NOT LIKE '%sc ~%' and description NOT LIKE '%sc;%' and description NOT LIKE '%sc~%' and description NOT LIKE '%sc/%' and description NOT LIKE '%snapchat:%' and description NOT LIKE '%snapchat - %' and description NOT LIKE '%snapchat-%' and description NOT LIKE '%snap~%' and description NOT LIKE '%snap =%' and description NOT LIKE '%snap•%' and description NOT LIKE '%snap%' and description NOT LIKE '%snap~%' and description NOT LIKE '%snapchat%' and description NOT LIKE '%snapchat ~ %' and description NOT LIKE '%snap:%' and description NOT LIKE '%add my sc%' and description NOT LIKE '%add me on sc%' and description NOT LIKE '%add me sc%' and description NOT LIKE 'snap %')

1 个答案:

答案 0 :(得分:0)

您在此使用\作为ESCAPE字符:

  description NOT LIKE '%sc\_%' ESCAPE '\'

这是一个有问题的选择,因为\本身在MySQL's string-literal level处以及各种客户端软件包中都被用作转义字符。

如果这是我的代码,那么在这种情况下我将完全不信任\(因为我很懒,并且我不想弄清楚它在哪里以及如何用作转义字符)。

最好,选择其他这样的转义字符(在此示例中为|

  description NOT LIKE '%sc|_%' ESCAPE '|'

或在SQL的ESCAPE部分中将\翻倍,像这样

  description NOT LIKE '%sc\_%' ESCAPE '\\'

(而且请记住,column LIKE '%whatever'是一个臭名昭著的查询性能反模式,因为它不能使用column上的任何索引。)