I am trying to understand this mySQL query. It is assumed that he searches for “elements” of the table with the search term “brown fox lazy dog” and returns a result based on its relevance or the number of times this occurs in the records.
SELECT * FROM `items` WHERE `description` LIKE 'quick' AND ( `description` LIKE 'brown' OR `description` LIKE 'fox' OR `description` LIKE 'lazy' OR `description` LIKE 'dog' ) ORDER BY ( ( CASE WHEN `description` LIKE 'brown' THEN 1 ELSE 0 END ) + ( CASE WHEN `description` LIKE 'fox' THEN 1 ELSE 0 END ) + ( CASE WHEN `description` LIKE 'lazy' THEN 1 ELSE 0 END ) + ( CASE WHEN `description` LIKE 'dog' THEN 1 ELSE 0 END ) ) DESC LIMIT 0, 30
The part that I don’t understand is in the part in the ORDER BY and CASE clauses. Say that a particular record matches all 4 keywords that we are looking for, do we get ORDER BY (4)? How is this 4 related to a particular row? As far as I understand, ORDER BY is usually used in a column, not in a number? Thank you
source share