如何像“\detail1\detail2\”一样应用 SQL? (逃避'\')?

发布于 2024-11-30 09:35:57 字数 581 浏览 1 评论 0原文

我有一个表 T1 ,其中有一列 details,数据列具有以下示例数据:

select details from T1;

\this is detail 1\this is detail2\
\this is detail 1\this is detail2\
:this is detail 1:this is detail2:
:this is detail 1:this is detail2:
\this is detail 1\this is detail2\

我只想获取那些由 < 分隔的 details代码>\。

为此,我编写了以下查询:

select details from T1 where details like '\%\%\';

但它没有按预期响应,因为它采用 \ 作为转义校正器。

所以,它显示了不完整的单个 inverted(') 错误!

那么,该怎么做呢?

I have a table T1 with a column details, data column has following sample data :

select details from T1;

\this is detail 1\this is detail2\
\this is detail 1\this is detail2\
:this is detail 1:this is detail2:
:this is detail 1:this is detail2:
\this is detail 1\this is detail2\

I want to fetch only those details which are separated by \.

For that I wrote the following query :

select details from T1 where details like '\%\%\';

But its not responding as expected, since its taking \ as escape corrector.

So, its showing error of incomplete single inverted(') !

So, how to do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最单纯的乌龟 2024-12-07 09:35:57

试试这个,你需要在 LIKE 语句中转义反斜杠两次。

select details from T1 where details like '\\\\%\\\\%\\\\'

http://dev.mysql.com/doc/refman /5.0/en/string-comparison-functions.html

由于 MySQL 在字符串中使用 C 转义语法(例如,“\n”表示换行符),因此必须将 LIKE 字符串中使用的任何“\”加倍。例如,要搜索“\n”,请将其指定为“\\n”。要搜索“\”,请将其指定为“\\\\”;这是因为反斜杠被解析器剥离一次,并在进行模式匹配时再次剥离,留下一个反斜杠进行匹配。

Try this, you need to escape backslashes twice in LIKE statement.

select details from T1 where details like '\\\\%\\\\%\\\\'

http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.

秋风の叶未落 2024-12-07 09:35:57

MySql 有用于反斜杠的转义字符 \\(与类 C 语言相同):

http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

MySql has escape character \\ for backslash (same as in C-like languages):

http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

北斗星光 2024-12-07 09:35:57

尝试使用 '\\%\\%\\' - 使用反斜杠来转义反斜杠。

Try using '\\%\\%\\' - using a backslash to escape the backslash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文