与MYSQL中的多个字段和空格类似的通配符 [英] LIKE wildcard with multiple fields and spaces in MYSQL

查看:44
本文介绍了与MYSQL中的多个字段和空格类似的通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个字段中搜索任何相似的比赛时遇到了一些麻烦. 例如,我有一个值如下的表:

I'm having some trouble searching for any similar match in two fields. For example I have a table with the values:

CAR MAKE   CAR MODEL
Ford       Mustang (Shelby)
Toyota     Corolla
Seat       Leon

etc etc.

我希望能够通过搜索以下任意组合来获得结果"Ford,Mustang(Shelby)":

I want to be able to get the result "Ford, Mustang (Shelby)" by searching for any of the following combinations:

  • 福特
  • 野马
  • 谢尔比
  • 福特野马

  • Ford
  • Mustang
  • Shelby
  • Ford Mustang

或任何其他组合.

这可能吗? 我的搜索很好,但是很难找到描述我的意思的搜索字词.

Is this possible? I've had a good search but it's hard to find the search terms to describe what I mean.

推荐答案

将您的术语放在空格上,然后为每个术语构建一点这样的SQL:

Split your terms on whitespace and then, for each term, build a little bit of SQL like this:

car_make like '%x%' or car_model like '%x%'

然后将所有这些与or结合在一起以获取WHERE子句.因此,对于"Shelby Ford",您将得到如下所示的SQL:

Then join all of those with or to get your WHERE clause. So for "Shelby Ford", you'd end up with SQL like this:

select stuff
from cars
where car_make like '%Shelby%'
   or car_model like '%Shelby%'
   or car_make like '%Ford%'
   or car_model like '%Ford%'

如果您需要更复杂的内容,请研究MySQL的全文搜索功能.

If you need anything more complicated then investigate MySQL's full-text search capabilities.

这篇关于与MYSQL中的多个字段和空格类似的通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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