<bdo id='TOmSu'></bdo><ul id='TOmSu'></ul>

  • <i id='TOmSu'><tr id='TOmSu'><dt id='TOmSu'><q id='TOmSu'><span id='TOmSu'><b id='TOmSu'><form id='TOmSu'><ins id='TOmSu'></ins><ul id='TOmSu'></ul><sub id='TOmSu'></sub></form><legend id='TOmSu'></legend><bdo id='TOmSu'><pre id='TOmSu'><center id='TOmSu'></center></pre></bdo></b><th id='TOmSu'></th></span></q></dt></tr></i><div id='TOmSu'><tfoot id='TOmSu'></tfoot><dl id='TOmSu'><fieldset id='TOmSu'></fieldset></dl></div>
  • <tfoot id='TOmSu'></tfoot>

      <small id='TOmSu'></small><noframes id='TOmSu'>

      <legend id='TOmSu'><style id='TOmSu'><dir id='TOmSu'><q id='TOmSu'></q></dir></style></legend>

        为什么我需要双重转义(使用 4 )才能在纯 SQL 中找到反斜杠 ()?

        时间:2023-10-08
          <tbody id='N1l4a'></tbody>

        • <bdo id='N1l4a'></bdo><ul id='N1l4a'></ul>

          <legend id='N1l4a'><style id='N1l4a'><dir id='N1l4a'><q id='N1l4a'></q></dir></style></legend>
          • <small id='N1l4a'></small><noframes id='N1l4a'>

              <tfoot id='N1l4a'></tfoot>

                  <i id='N1l4a'><tr id='N1l4a'><dt id='N1l4a'><q id='N1l4a'><span id='N1l4a'><b id='N1l4a'><form id='N1l4a'><ins id='N1l4a'></ins><ul id='N1l4a'></ul><sub id='N1l4a'></sub></form><legend id='N1l4a'></legend><bdo id='N1l4a'><pre id='N1l4a'><center id='N1l4a'></center></pre></bdo></b><th id='N1l4a'></th></span></q></dt></tr></i><div id='N1l4a'><tfoot id='N1l4a'></tfoot><dl id='N1l4a'><fieldset id='N1l4a'></fieldset></dl></div>
                  本文介绍了为什么我需要双重转义(使用 4 )才能在纯 SQL 中找到反斜杠 ()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我不理解这种 MySQL 行为:如果我想显示 a,我可以选择 "a\b" ,它可以正常工作:

                  I do not understand this MySQL behaviour : if I want to display a, I can just select "a\b" which work without problem :

                  mysql> select "a\b";
                  +-----+
                  | a |
                  +-----+
                  | a |
                  +-----+
                  1 row in set (0.05 sec)
                  

                  但是如果我想使用 LIKE 在表中搜索包含 的字符串,我需要双重转义我的".为什么?

                  But if I wnat to search a string containing a in a table using LIKE, I need to double-escape my "". Why ?

                  这是一个例子.

                  我们准备了一张小桌子.

                  We prepare a small table.

                  create table test ( test varchar(255) );
                  insert into test values ( "a\b" ) , ( "a\b\c" ) , ( "abcd" );
                  
                  mysql> select * from test;
                  +-------+
                  | test  |
                  +-------+
                  | a   |
                  | ac |
                  | abcd  |
                  +-------+
                  3 rows in set (0.05 sec)
                  

                  我们尝试获取以a"开头的条目......

                  We try to get entries beginning by "a" ...

                  mysql> select * from test where test LIKE "a\b%";
                  +------+
                  | test |
                  +------+
                  | abcd |
                  +------+
                  1 row in set (0.05 sec)
                  

                  为什么 \ 在那里被忽略?为什么我需要双重转义 basckslash 才能得到预期的结果?

                  Why \ is just ignored there? Why I need to double-escape basckslash to get my expected result?

                  mysql> select * from test where test LIKE "a\\b%";
                  +-------+
                  | test  |
                  +-------+
                  | a   |
                  | ac |
                  +-------+
                  2 rows in set (0.04 sec)
                  

                  推荐答案

                  你先转义字符串语法,然后转义 LIKE 语法.

                  You escape first for the string syntax, then for LIKE syntax.

                  LIKE 中的字符%_ 有特殊含义,所以如果要搜索字面量%,你需要使用 \%,如果你想搜索文字 \% 你需要像 \% 一样转义反斜杠.

                  In LIKE characters % and _ have special meaning, so if you want to search for literal %, you need to use \%, and if you want to search for literal \% you need to escape the backslash as in \%.

                  在字符串语法中 " 显然有特殊含义,所以如果你想在字符串中包含引号,你需要将它转义为 ",并包含文字 " 在字符串中,您必须像 \" 一样对反斜杠进行转义.

                  In string syntax " obviously has special meaning, so if you want to include quote in the string you need to escape it as ", and to include literal " in the string you have to escape the backslash as in \".

                  所以在这两种语法中你都必须转义 .

                  So in both syntaxes you have to escape .

                  如果不想使用 转义 LIKE 模式,可以使用 ESCAPE 关键字.例如:

                  If you don't want to use to escape the LIKE pattern , you can use ESCAPE keyword. For example:

                  ...  where test LIKE "a\b%" ESCAPE '|';
                  

                  这样,您需要编写 |%|_|| 来转义这些特殊字符.

                  This way, you'll need to write |%, |_ or || to escape these special chars.

                  这篇关于为什么我需要双重转义(使用 4 )才能在纯 SQL 中找到反斜杠 ()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从同一个表中获取员工姓名和经理姓名的 SQL 查询 下一篇:MySQL IN 和 LIKE

                  相关文章

                1. <i id='0apUs'><tr id='0apUs'><dt id='0apUs'><q id='0apUs'><span id='0apUs'><b id='0apUs'><form id='0apUs'><ins id='0apUs'></ins><ul id='0apUs'></ul><sub id='0apUs'></sub></form><legend id='0apUs'></legend><bdo id='0apUs'><pre id='0apUs'><center id='0apUs'></center></pre></bdo></b><th id='0apUs'></th></span></q></dt></tr></i><div id='0apUs'><tfoot id='0apUs'></tfoot><dl id='0apUs'><fieldset id='0apUs'></fieldset></dl></div>
                  • <bdo id='0apUs'></bdo><ul id='0apUs'></ul>

                  1. <tfoot id='0apUs'></tfoot>

                    <small id='0apUs'></small><noframes id='0apUs'>

                      <legend id='0apUs'><style id='0apUs'><dir id='0apUs'><q id='0apUs'></q></dir></style></legend>