Jump to content

Complex ORDER BY help


Canman2005

Recommended Posts

Hi all

 

I have the following database table

 

ID  VALUE

1    Apple

2    Microsoft

3    Mobiles

4    MP3s

5    Laptops

 

I use a "SELECT" query in my PHP to get those results.

 

Is it possible to "ORDER" my results by

 

ID number 2 first

 

ID number 1 second

 

and then the remainder of the results in a A-Z order

 

so the above would look like

 

ID  VALUE

2    Microsoft

1    Apple

5    Laptops

3    Mobiles

4    MP3s

 

I tried

 

ORDER BY  (CASE WHEN `id` = 2 THEN 0 WHEN `id` = 1 THEN 2 END), `value` ASC

 

But that doesn't seem to work no matter how much I tweak it around

 

Any help would be great

 

Thanks

 

Ed

Link to comment
Share on other sites

What difference would that make? You can easily add a new column called "ordering" if you want to order them in a particular order.

 

You can still do it using UNION. Just select the first 2 in descending order, then UNION it with the rest ordered by descending.

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.