MYSQL Search / Wildcards

I am trying to find a MySQL database using PHP, the search works fine, but I am looking for a little help using wildcards:

The data in the (Model) field I'm looking for is: "A4" (215 results)

My search string:

SELECT * FROM `temp_comp`.`mvl` WHERE `Model` LIKE '%A4 Avant%' 

Is there a way I can find β€œA4 Avant”, but that will return any fields containing β€œA4” or β€œAvant”

The search query was taken from the csv file, so I wanted to try and do it without first breaking two words and not distorting "A4" and / or "Avant", I tried the following, but did not get the results

 SELECT * FROM `temp_comp`.`mvl` WHERE `Model` LIKE '%A4%Avant%' 

As you may have guessed, this is not my normal field, so any help would be greatly appreciated.

+4
source share
3 answers
 SELECT * FROM temp_comp.mvl WHERE (Model LIKE '%A4%') OR (Model LIKE '%Avant%') 

If you want to avoid splitting the test, you can use regexp:

 SELECT * FROM temp_comp.mvl WHERE Model REGEXP 'A4|Avant' <<-- Either '%A4% or %Avant% SELECT * FROM temp_comp.mvl WHERE Model REGEXP 'A4*Avant' <<-- '%A4%Avant% 

See link

+6
source

MySQL does not have a built-in way of breaking lines in a query. Separating them in advance and then adding n predicates to the where clause is trivial.

 SELECT * FROM `temp_comp`.`mvl` WHERE `Model` LIKE '%A4%' OR `Model` LIKE '%Avant%'; 
0
source

Here you can achieve this.

Modify your query as follows:

 SELECT * FROM `temp_comp`.`mvl` WHERE `Model` LIKE '%A4%' AND Model LIKE '%Avant%' 

In the above example, they will search for the elements of the search query in the query so that they are all present in the record. And if you want to search for records with one of the search conditions, you can replace the word AND with OR , and it performs the task.

And ideally, you should follow these steps to prepare your request:

  • Take the input line to search.
  • Separate it with spaces so you have an array of words
  • Scroll through the array and create a search string.
  • Use the search bar in the query.
0
source

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


All Articles