Bug #32425 sorting based on parameter input
Submitted: 16 Nov 2007 2:56 Modified: 4 Feb 2009 15:13
Reporter: Gary Thomann Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Parser Severity:S4 (Feature request)
Version:4.1, 5.1 OS:Any
Assigned to: CPU Architecture:Any

[16 Nov 2007 2:56] Gary Thomann
Description:
Wanting to sort based on parameter input and with muliple columns.
This is for crystal reports usage but applies to database usage too.

How to repeat:
Base script...
select    t.col1
         ,t.col2
from      table t
order by 2,1; 
Column placing works.

select    t.col1
         ,t.col2
from      table t
order by (if (FALSE
             ,1
             ,2)
         ); 
-> dont work; should sort by col2, compiles but no sorting effected.  sorts by PK as default.  ie col1

Now lets try case statement...
select    t.col1 as col1
         ,t.col2 as col2
         ,t.col3 as col3
from      report rep
order by (case '{?Sort Choice}'
          when 'choice1' then Col1
          when 'choice2' then Col2
                         else Col3 end
         ); 
This works.

So now try multiple columns... which dont work.
select    t.col1 as col1
         ,t.col2 as col2
         ,t.col3 as col3
from      report rep
order by (case '{?Sort Choice}'
          when 'choice1' then Col1,Col2
          when 'choice2' then Col2,Col1
                         else Col1,Col2,Col3 end
         ); 

Same story with using column placing...
select    t.col1 as col1
         ,t.col2 as col2
         ,t.col3 as col3
from      report rep
order by (case '{?Sort Choice}'
          when 'choice1' then 1,2
          when 'choice2' then 2,1
                         else 1,2,3 end
         );
[16 Nov 2007 6:16] Gary Thomann
... from report rep  should read   from  table tab
[16 Nov 2007 6:19] Gary Thomann
... from report rep  should read   from  table t

mmm Edit Submission would be nice!
[4 Feb 2009 15:13] Susanne Ebrecht
Many thanks for writing a feature request.

This is related to functions and regular expression in order by.

Our development will discuss the implementation.