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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.