Results 1 to 4 of 4

Thread: [RESOLVED] MySQL stored procedure, order by with condition

  1. #1

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Resolved [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
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: MySQL stored procedure, order by with condition

    This may work:
    Code:
    ORDER BY IF (VariableA > VariableB , -1 * C1, C1) ASC ;

  4. #4

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: MySQL stored procedure, order by with condition

    Superb
    thanks for the help Tech and Geek
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width