-
Nov 13th, 2017, 10:12 AM
#1
Thread Starter
PowerPoster
[RESOLVED] MySQL stored procedure, order by with condition
Code:
SELECT C1,C2,C3 FROM Tbl
ORDER BY
IF (VariableA > VariableB , C1 DESC , C1 ASC) ;
-- Here VariableA and VariableB are the variables declared in
-- Stored procedure
but this is syntax error how can i do it
-
Nov 13th, 2017, 10:35 AM
#2
Re: MySQL stored procedure, order by with condition
I don't think it's that simple... you could swap between two fields/values, but they'd have the same DESC/ASC sort... you're looking to swap the sort order, which you can't just arbitrarily switch like that. What you'll probably need to do is have TWO select statements, exactly the same, but with the different sort orders, and then call one or the other based on your variables:
Code:
If VarA > VarB
SELECT C1,C2,C3 FROM Tbl
ORDER BY C1 DESC
else
SELECT C1,C2,C3 FROM Tbl
ORDER BY C1 ASC
-tg
-
Nov 13th, 2017, 10:41 AM
#3
Re: MySQL stored procedure, order by with condition
This may work:
Code:
ORDER BY IF (VariableA > VariableB , -1 * C1, C1) ASC ;
-
Nov 13th, 2017, 10:49 AM
#4
Thread Starter
PowerPoster
Re: MySQL stored procedure, order by with condition
Superb
thanks for the help Tech and Geek
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|