gpt4 book ai didi

mysql - 在 MySQL 中搜索具有特殊字符的字符串值

转载 作者:行者123 更新时间:2023-11-29 00:09:57 32 4
gpt4 key购买 nike

我在 MySQL 数据库中有一个表 emp,它的名称是一列。在该名称列中,值为“abc\xyz”。我想搜索这个值。我尝试使用以下查询:

select * from emp where name like 'abc\xyz';

我也试过了

select * from emp where name like 'abc\xyz' escape '\\';

但是我没有找到任何输出。你能帮我找到这样的字符串吗?这样的字符串可以在任何位置具有特殊字符。

提前致谢。

最佳答案

你可以这样尝试:

select * from emp
where empname like '%abc\\\\xyz%'

SQL Fiddle Demo

来自docs :

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.

关于mysql - 在 MySQL 中搜索具有特殊字符的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25637575/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com