MySQL选择带有特殊字符的字符串 [英] MySQL selecting string with special characters

查看:747
本文介绍了MySQL选择带有特殊字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从数据库中选择字符串时遇到问题.问题是如果您在行中有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 replaceing 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆