MySQL选择带有特殊字符的字符串 [英] MySQL selecting string with special characters
问题描述
我在从数据库中选择字符串时遇到问题.问题是如果您在行中有McDonald's
,并且您正在使用字符串mcdonalds
搜索,它将找不到任何结果.有什么建议吗?
I'm having a problem selecting strings from database. The problem is if you have McDonald's
in row and if you are searching with a string mcdonalds
it wouldn't find any results. Any suggestions?
我忘了说我在WHERE
句子中使用了LIKE
.
I forgot to mention that I'm using LIKE
in WHERE
sentence.
推荐答案
如果您的搜索要求是忽略某些字符,则可以在搜索过程中通过用空白replace
对其进行删除来将其删除.
If your search requirements are to ignore certain characters, you can remove them during a search by replace
ing them with a blank.
此答案解决了您的问题:
This answer solves your problem:
SELECT *
FROM restaurants
WHERE replace(name, '''', '') like '%mcdonalds%'; -- This will match "McDonald's"
FYI,将单引号文字('
)写为双单引号(''
),因此要将单引号指定为replace
的参数,您需要连续四个引号(
FYI, a single quote literal ('
) is written as a doubled single quote (''
), so to specify a single quote as a parameter to replace
you need four quotes in a row (''''
) - two at each end and the doubled quote in the middle for the actual quote.
这篇关于MySQL选择带有特殊字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!