EchoFool Posted December 18, 2009 Share Posted December 18, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/ Share on other sites More sharing options...
cags Posted December 18, 2009 Share Posted December 18, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-979751 Share on other sites More sharing options...
fenway Posted December 18, 2009 Share Posted December 18, 2009 You should just use ORDER BY FIELD() instead. Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-979782 Share on other sites More sharing options...
EchoFool Posted December 18, 2009 Author Share Posted December 18, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-979889 Share on other sites More sharing options...
fenway Posted December 18, 2009 Share Posted December 18, 2009 Yup... as long as there are no other values for AccountType, that will work -- a non-match will return 0, so it gets more complicated. Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-979898 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 i agreee with the above poster. more thaan anything Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-979954 Share on other sites More sharing options...
EchoFool Posted December 18, 2009 Author Share Posted December 18, 2009 I get this error: FUNCTION AccountType does not exist For: ORDER BY Flagged DESC, Redeemed ASC, AccountType ('1 Month','2 Month','1 Year','2 Year') Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-980142 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 ur asking for something that u don have. the work around is take the thrid variable then check each individial one until u finish which one is wrong check Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-980144 Share on other sites More sharing options...
PFMaBiSmAd Posted December 18, 2009 Share Posted December 18, 2009 Your query is missing the actual FIELD() statement. Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-980166 Share on other sites More sharing options...
EchoFool Posted December 18, 2009 Author Share Posted December 18, 2009 So: ORDER BY Flagged DESC, Redeemed ASC, Field(AccountType ('1 Month','2 Month','1 Year','2 Year')) Like that? Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-980180 Share on other sites More sharing options...
fenway Posted December 18, 2009 Share Posted December 18, 2009 No: ORDER BY Flagged DESC, Redeemed ASC, Field(AccountType ,'1 Month','2 Month','1 Year','2 Year') Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-980203 Share on other sites More sharing options...
EchoFool Posted December 18, 2009 Author Share Posted December 18, 2009 Oh thanks - works perfect ! Quote Link to comment https://forums.phpfreaks.com/topic/185567-order-by-specific-list/#findComment-980292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.