我的一个表中有一个字段包含此字符串:(
!"#¤%&/()=?´`?=)(/&%¤#"!\'\'"'
当然仅用于测试目的)。我尝试了无数的查询来正确选择该字段,当然不会返回任何错误,但我似乎无法正确选择。
这是我当前正在使用的查询:
SELECT * FROM mytable WHERE `column` LIKE '%!"#¤%&/()=?´`?=)(/&%¤#"!\\\'\\\'"\'%'
任何人都可以阐明我做错了什么吗?是否还有其他字符('
除外)需要转义?我没有在任何地方读过它......(但是我尝试在前面的符号之前添加反斜杠)。
I have a field in one of my tables that contains this string:
!"#¤%&/()=?´`?=)(/&%¤#"!\'\'"'
(Only for test purposes ofcourse). I've tried endless of queries to properly select this field, and without returning any errors of course, but I just can't seem to get it right.
This is the query I'm using currently:
SELECT * FROM mytable WHERE `column` LIKE '%!"#¤%&/()=?´`?=)(/&%¤#"!\\\'\\\'"\'%'
Can anyone shed some light on what it is I'm not doing right? Are there any other characters (other than '
) that I should escape? I haven't read about it anywhere... (I did however try adding backslashes before the precent symbols).
发布评论
评论(5)
来自 MySQL 手册:
因此,您应该分两步转义
LIKE
运算符的字符串。在 PHP 中,它可以是这样的:
更新:
使用 MySQLi
使用 PDO
或者您可以使用 PDO 准备好的语句,而不是第二步(文字转义):
From MySQL Manual:
So, you should escape string for
LIKE
operator in two steps.In PHP it can be like this:
UPDATE:
Use MySQLi
Use PDO
Or you can use PDO prepared statement, instead of second step (literal escaping):
不清楚您想要获得什么以及出了什么问题。
顺便说一句,如果你想保护你的查询免受 SQL 注入,你应该使用 mysql_real_escape_string
http://dev.mysql.com/doc /refman/5.0/en/mysql-real-escape-string.html
假设你在 PHP
但你必须记住 LIKE 运算符有他自己的特殊字符(通配符)
http://dev.mysql.com/doc /refman/5.0/en/string-comparison-functions.html#operator_like
因此,如果你想阻止它们的魔力,这个字符必须用反斜杠转义
假设你在 PHP 中,我会这样做
Is not clear what you are trying to obtain and what is going wrong.
By the way, if you want to protect your query from SQL injection you should use mysql_real_escape_string
http://dev.mysql.com/doc/refman/5.0/en/mysql-real-escape-string.html
Assuming that you are in PHP
But you have to remember that LIKE operator has his own special chars (wildchars)
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
So this chars must be escaped with backslash if you want to stop their magic
Assuming that you are in PHP I would do
在某些情况下,使用 LOCATE 更简单: https://www.w3schools.com/sql/func_mysql_locate .asp
我不能确定哪个更快:MySQL LIKE 与 LOCATE
ORM 也不是问题:
对于 99% 的项目,LOCATE 比 LIKE 是更好的解决方案。您不会因特殊字符而头痛。您需要 LIKE 的主要原因只是全文搜索,如果您使用 LIKE 来实现此目的,则意味着您不需要为此进行特殊的操作。
In some cases it's simpler to use LOCATE: https://www.w3schools.com/sql/func_mysql_locate.asp
I can't say for sure what is faster: MySQL LIKE vs LOCATE
ORM is not a problem too:
For 99% projects LOCATE would be better solution than LIKE. You wouldn't have headache with special characters. The main reason why do you need LIKE is just a full text search and if you use LIKE for that purpose then it means you don't need something special for that.
您使用 PHP 吗?如果是这样,你可以尝试这样的事情:
这能解决你的问题吗?
Are you using PHP? If so, you may try something like:
Would that solve your problem?
如果您要比较的字段(模式)不是常量(如您指定的常量:
%!"#¤%&/()=?...
),您可以像这样直接在 MySQL 中转义它:(其中
pattern
可以是另一列:column2
、MySQL 变量或表达式)这也很有用在存储过程/函数中。
If that field that are you comparing with (the pattern), is not a constant (like the one which you've specified:
%!"#¤%&/()=?...
), you can escape it directly in MySQL like this:(where
pattern
can be either another column:column2
, a MySQL variable, or an expression)This can be also useful in a stored procedure/function.