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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -