My idea is to use a query like this:
set @search = 'red dress'; SELECT * FROM products INNER JOIN colors ON products.color_id = colors.id WHERE (FIND_IN_SET(title, REPLACE(@search, ' ', ','))>0)+ (FIND_IN_SET(color, REPLACE(@search, ' ', ','))>0) = LENGTH(@search)-LENGTH(REPLACE(@search, ' ', ''))+1;
Two replacements inside FIND_IN_SET are used to create a list of properties separated by commas:
red,dress
Then I check to see if there is a title in this set. If present, the value is:
(FIND_IN_SET(title, REPLACE(@search, ' ', ','))>0)
will be evaluated at 1, 0 otherwise. The same goes for color .
The number of properties in the search bar can be calculated as:
LENGTH(@search)-LENGTH(REPLACE(@search, ' ', ''))+1
(yes, this is a dirty trick!). If the number of matching properties matches the properties in the @search string, the string will be returned. Please note that performance will be poor.
Fiddle is here .
source share