Jump to content

ORDER BY specific list?


EchoFool

Recommended Posts

Hey,

 

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 *, 

(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' 

ORDER BY sort ASC")

  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
https://forums.phpfreaks.com/topic/185567-order-by-specific-list/
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;
END CASE

 

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());

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.