Skip to content
Advertisement

MYSQL query for first 40 records and else records

I have a table names ‘tbl_featured_professional’ where fields are:

id,
user_id,
ranking and 
score,
createdDate 

What I want is first 40 records are ordered from ranking (which is unique) and all other records after 40 which are ordered by score. I want to do it from mysql and not from PHP. How can I do this? Thank you.

Advertisement

Answer

If ranking — as its name implies — is a number starting at 1 and incrementing, then:

select fp.*
from tbl_featured_professional fp
order by (fp.ranking <= 40) desc,
         (case when fp.ranking <= 40 then ranking end),
         score;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement