mysqlsqlwhere-clausesql-likesoundex

ORDER BY soundex with WHERE (MySql)


Query code:

SELECT *
FROM example
WHERE name LIKE '%test%'
OR SOUNDEX(name) LIKE 'T230%'
OR SOUNDEX(name) LIKE 'T23%'

I want to show first the results matched with WHERE name LIKE '%test%' and after SOUNDEX(name) LIKE 'T230%' and the lasts rows is the result of SOUNDEX(name) LIKE 'T23%'

Thank you for the attention.


Solution

  • You can use boolean expressions in order by. The "true" is treated as "1" and false as "0". So:

    ORDER BY (name LIKE '%test%') DESC,
             (SOUNDEX(name) LIKE 'T23%') DESC,
             (SOUNDEX(name) LIKE 'T230%') DESC