LIKE wildcard with multiple fields and spaces in MYSQL

I am having trouble finding any similar match in two fields. For example, I have a table with values:

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

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

  • Ford
  • Mustang
  • Shelby
  • Ford mustang

    or any other combination.

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

+6
source share
2 answers

Separate your words in a space, and then create a little SQL for each term, like this:

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

Then attach all those with or to get the WHERE clause. So, for "Shelby Ford" you get SQL as follows:

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

If you need something more complex, explore the possibilities of MySQL full-text search .

+7
source

Try:

 SELECT Car_Make, Car_Model, CONCAT_WS(' ', Car_Make, Car_Model) as Make_Model FROM cars WHERE CONCAT_WS(' ', Car_Make, Car_Model) LIKE '%Ford Mustang%' 

Not sure about the exact syntax since I'm not at home, but something like this should work.

See also: Using mysql concat () in a WHERE clause?

+2
source

Source: https://habr.com/ru/post/895468/


All Articles