In the database:

id vstavki 1 фианит,топаз, бриллиант 2 гранат,аметист 3 раухтопаз 4 аметист, топаз 5 london-топаз, фианит 

My request:

 SELECT * FROM price WHERE section=1 AND price BETWEEN 600 AND 1500000 AND size IN(16,16.5,17) AND material IN("Золото","Серебро") AND (vstavki LIKE "%Бриллиант%" OR vstavki LIKE "%Топаз%") group by nomer 

However, rauchtopaz falls into the conclusion. How do you need to write a request so that rauchtopaz does not get into the output when sampling by topaz?

  • remove % in vstavki LIKE "%Топаз%" - Bald
  • then they will not get into position output id = 1 id = 4 id = 5 - amijin

2 answers 2

Generally so:

 select * from price where lower(vstavki) regexp '(^|[^a-zа-яр-я])(топаз|бриллиант)([^a-zа-я]|$)'; 

BUT, pay attention, I had to include in the first square brackets, excluding any letters before the words, besides the full range а-я included the р-я without it, he constantly found “rauchtopaz” because he considered the letter “x” not in the range а-я , but at the same time perfectly understood that it is in the р-я range (I selected the range experimentally). What it can be connected with, I do not know. But since he had one glitch with Russian letters, maybe something else. Those. I will not give a 100% guarantee that it will work with arbitrary words.

In any case, I applied lower lowering strings to small letters, so the search words in a regular expression should also be small letters, as an option, lower can also be used for them.

In addition, you may need to adjust the ranges of the letters of exceptions under your understanding of what should fall under the concept of a merged word and what is not.

In general, this is a reason to think about the structure of the base, even if filling out this field does not allow there to write anything, but to request each material separately and form the line itself with the correct delimiters. Although, if it’s very good, there should be a reference book of permissible materials for the inserts and a price record with individual records lying inserts to the given price line.

And yet, perhaps, you need to look towards sphinx full-text search, or at least built into MySQL

  • Exactly what is needed. Thank. - amijin
  • With the end of the s does not work: (Tell me how to fix it. - amijin
  • obviously add the letter ы to the set of letters, which at the end, i.e. something like ([^a-zа-яы]|$) . Although it is possible to add just р-я , it can help as in the first part. - Mike
  • It does not pass either (( - amijin
  • probably have to redo everything through LIKE. - amijin
 AND (vstavki LIKE "%Бриллиант%" OR vstavki LIKE "% Топаз%" OR vstavki LIKE "%,Топаз%") group by nomer 

Add a space before "Topaz". Now only words will be selected in which the fragment "Topaz" comes first

UPD Add another comparison option, with a comma without a space, to match this line:

1 zirconia, topaz, diamond

  • also an option, but I was hoping for a shorter version, perhaps with a regular schedule. - amijin
  • @amijin, it was necessary to indicate this in the question - Artem Y