mysql - Weighted conditions in the WHERE clause of a SQL statement -
i want search records particular field either starts string (let's "ar") or field contains string, "ar".
however, consider 2 conditions different, because i'm limiting number of results returned 10 , want starts condition weighted more heavily contains condition.
example:
select * employees name 'ar%' or name '%ar%' limit 10
the catch is if there names start "ar" should favored. way should name merely contains "ar" if there less 10 names start "ar"
how can against mysql database?
you need select them in 2 parts, , add preference tag results. 10 each segment, merge them , take again best 10. if segment 1 produces 8 entries, segment 2 of union product remaining 2
select * ( select *, 1 preferred employees name 'ar%' limit 10 union select * ( select *, 2 employees name not 'ar%' , name '%ar%' limit 10 ) x ) y order preferred limit 10
Comments
Post a Comment