MySQL “Advanced Where” Query
-
02-07-2021 - |
Question
I would like to know is there any possible way, whereby MySQL query can dynamically search whatever is there in the WHERE
clause.
For example if the query is
Select *
from table
where item = 'Dell Optiplex 390'
It should search for rows having:
Dell Optiplex 390
Dell Desktop Optiplex 390
Dell Laptop Optiplex 390
Optiplex dual core 390
etc., etc.
Solution
For these sort of search capabilities I think you are looking for a matching algorithm. You can use the Levenshtein Distance Algorithm, there is a php function levenshtein
that implement this algorithm if you are using php, to get the matching data and there is also Needleman–Wunsch algorithm, and if your data is in English only and one word column you can use Soundex Algorithm.
This is a start, you will find a lot of implementations out there for these algorithms in Mysql.
OTHER TIPS
If you need to know that field consist any of you words.
Select *
from table
where item like '%Dell%' OR item like '%Optiplex%' or item like '%390%'
There was a good answer here which was deleted, I would modify that answer to:
Select * from table where
lower(item) like '%dell%optiplex%390%' or
lower(item) like '%optiplex%390%' or
lower(item) like '%dell%laptop%390%' or ...
Use the SQL LIKE
clause with wild cards:
SELECT *
FROM table
WHERE item LIKE '%Dell%Optiplex390%';
Here's a good reference guide: