I have a MySQL table and would like to return the rows based on a column value in this order:
- First ascending order if the column >=0
- Then descending order if the column <0
E.g. 0,2,4,7,-2,-3,-5
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Can use SIGN to sort the positive numbers to the top, then take the absolute value with ABS to get the desired ASC/DESC.
EDIT
As Nahuel pointed out, the above will sort 0’s to the middle between positive and negative. To instead group them with the positives, you can use a
CASEinstead (or, if your column is only integers, the slightly magicalSIGN(col + 1))