简体   繁体   中英

Perfect SQL Query for Searching products

i am looking for the perfect SQL Query to search for products.

Now i am using WHERE LIKE '%...%' but i have problems that i dont found "related" products than.

Example: If i search Dampfbügeleisen (its german) than i only get results that includes exactly this word.

But i want also other products that has a thing of the word in his own name. Like bügeleisen

So, i will have Bügeleisen but also Dampfbügeleisen as result when i search Bügeleisen And also Dampfbügeleisen and Bügeleisen when i search for Dampfbügeleisen

Is something like that possible with just an SQL Query?

Use the LIKE with CONCAT and make all same case with LOWER:

SELECT product 
FROM products_table
WHERE 'Dampfbügeleisen' LIKE CONCAT('%', LOWER( product), '%') ;

You can check out more details on a similar question here:

https://dba.stackexchange.com/questions/203206/find-if-any-of-the-rows-partially-match-a-string

When someone looks for Dampfbügeleisen, you want to show mere Bügeleisen, too. Because of DampfBÜGELEISEN. Well, "Bügeleisen" is "iron" and "Dampfbügeleisen" is "steam iron". You will show irons that don't match what the user is actually looking for. You will also show Bügel (coat hanger). Or look for Kleiderständer (hat rack) and get Kleid (dress). I don't think this is a good idea.

SQL could do this, but this would take some effort. And where to start and where to stop? Dampfbügeleisen contains dampf (steam) and bügeleisen (iron, the tool) and bügel (coat hanger) and eisen (iron, the metal) and eis (ice / ice cream) and ei (egg).

What you really want is a more sophistcated database. This starts with a glossary. When looking for curtains you want to find drapes, too, and vice versa. When looking for pullovers you want to find sweaters and vice versa. When looking for pullovers, you want to find hoodies, too, but maybe not vice versa (because every hoody is a pullover, but not every pullover is a hoody). Well, not being a native English speaker I may be wrong here, but you get what I mean and probably can come up with better examples.

word1         | word2
--------------+-------------
pullover      | sweater
pullover      | hoody
sweater       | pullover
sweater       | hoody
curtain       | drape
drape         | curtain
...

The next step then is suggestions. Like when someone is looking for a steam iron you will show steam irons, but further down in the list you may offer plain irons. And you may show ironing boards next to the list. This needs again one or more tables that contain the relation between the products.

SQL is great for selecting data, but it does not know what a sweater, a pullover or an iron is. If and how these products are related is something you must tell the DBMS in your database tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM