Jump to content

ORDER BY specific list?


Recommended Posts



I been trying to make a MYSQL query to order by a specific list but i cannot get it to work this is whati  tried:


$Get = mysql_query("SELECT *, 

CASE WHEN `AccountType` = '1 Month' THEN 1
CASE WHEN `AccountType` = '3 Month' THEN 2
CASE WHEN `AccountType` = '1 Year ' THEN 3
CASE WHEN `AccountType` = '2 Year' THEN 4
ELSE 5 END) as sort

FROM table

WHERE Deleted='0' 


  Or die(mysql_error());



But currently i get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE WHEN `AccountType` = '3 Month' THEN 2CASE WHEN `AccountType` = '1 ' at line 3



Im trying to get the rows to sort by :


1 month

3 month

1 year

2 year

{every thing else}



What have i got wrong? Hope you can help!

Link to comment
Share on other sites

I've never used CASE WHEN syntax before, so please feel free to ignore me if I'm talking rubbish, but check the reference manually it seems to me like the syntax should be more along the lines of...


CASE AccountType
      WHEN '1 Month' THEN SELECT 1;
      WHEN '3 Month' THEN SELECT 2;
      WHEN '1 Year' THEN SELECT 3;
      WHEN '2 Year' THEN SELECT 4;


Link to comment
Share on other sites

You should just use ORDER BY FIELD() instead.


You mean like this:  ?


$Get = mysql_query("SELECT *  FROM table
                                  WHERE Deleted='0'
                                     ORDER BY AccountType ('1 Month','2 Month','1 Year','2 Year') ")

  Or die(mysql_error());

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

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

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.