Jump to content

Custom Sort Order or Default Results First


suttercain

Recommended Posts

Hi everyone,

 

I have a column with the following records:

 

UCLA

YALE

HARVARD

USC

 

 

I want USC to always appear first, or outputted at the top of the list. If I ORDER BY it will alphabetize the results and I won't get the desired result. I looked into using CASE but couldn't get it.

 

Does anyone know a way to have a specified result appear at the top of the list all the time?

 

Thanks!

Hi

 

You could use case.

 

However I assume that these records can appear multiple times (eg, a list of students with the facility that they are at)

 

If so it might be best to have another table listing the sort order for those items. Then JOIN your current table to that new table to get the sort order.

 

All the best

 

Keith

As kickstart mentioned your 2 best options are having a table specifying the order or another column in that table specifying the order. These would be best when it comes to optimization however you could also do

 

ORDER BY FIELD(school_name, 'USC') desc, school_name

 

school_name would be the name of your column containing... well the school names :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.