Jump to content

[SOLVED] Control what is displayed first?


kevin_newbie

Recommended Posts

Hello,

 

What is the best way to display an entry (Mysql and php) first when even though you have it set to alphabetical order?

 

For example i current have the list of a few states as followed;

 

Alabama

Alaska

Arizona

Arkansas

California

Colorado

 

But I want to be able to put California first then the rest follow so like this:

 

California

Alabama

Alaska

Arizona

Arkansas

Colorado

 

What is the best way to achieve that?

 

Thanks

Link to comment
Share on other sites

Hi

 

Depends how many you want like that and how flexible.

 

Personally I would add an extra column for primary sort order. Set the ones you want to appear first to have a sort order of 1 and the rest to 2 (or any other pair of numbers). Sort by that and then the name.

 

You could actually do it with a case statement within the SQL to generate an extra column for the sort order and recognise California, but while that would avoid having to do a simple database change it would be clunky and a pain to modify when you suddenly want (say) Texas to appear at the top as well .

 

All the best

 

Keith

Link to comment
Share on other sites

Mchl - That makes sense what union does now, but I only have one table not two.

 

kickstart - I went the route of adding another column and having everything have 2 while california have 1. When i do the query though it does california first then everything else not in alphabetical order?

 

SELECT id,name,orderList FROM states ORDER BY orderList ASC

 

I'm I missing something?

 

Thanks

Link to comment
Share on other sites

Hi

 

Using the extra column you could do:-

 

SELECT id,name,orderList FROM states ORDER BY orderList , name ASC

 

Using a UNION you could do:-

 

SELECT id,name, 1 AS isCal FROM states WHERE name = "California"

UNION

SELECT id,name, 2 AS isCal FROM states WHERE name != "California"

ORDER BY isCal, name ASC

 

Personally I would prefer using the extra column as it means you do not have data in the code, but either are valid.

 

All the best

 

Keith

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.