Point me to some references for Sorting

This is a discussion on Point me to some references for Sorting within the MySQL Database forums, part of the Database Forums category; TIA for you help, I have a query which works and sorts as expected. Now I need to do the ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-22-2006
Marty Meyers
 
Posts: n/a
Default Point me to some references for Sorting

TIA for you help,
I have a query which works and sorts as expected. Now I need to do the same
query, only this time I have two columns which need to be analysed and
ordered by:
The analysis is:(in php terms)(both columns are positive numbers)

if (ColA=0)
{ // use Col B}

elseif(ColA > 0 && colA < ColB)
{ // use Col A
}
else
{ //use ColB}

The MsSQL manual doesn't seem to cover this, nor does the two beginner
books I have. A pointer to a reference where I can learn about advanced
"Order by" would be great
Thanks
Marty


Reply With Quote
  #2 (permalink)  
Old 03-23-2006
Bill Karwin
 
Posts: n/a
Default Re: Point me to some references for Sorting

"Marty Meyers" <martym@execpc.com> wrote in message
news:1221g89ld0dtoab@corp.supernews.com...
> if (ColA=0)
> { // use Col B}
>
> elseif(ColA > 0 && colA < ColB)
> { // use Col A
> }
> else
> { //use ColB}


SELECT ...
ORDER BY CASE
WHEN ColA = 0 THEN ColB
WHEN ColA > 0 AND ColA < ColB THEN ColA
ELSE ColB
END

It's based on the fact that the arguments to ORDER BY are expressions, not
simply column names. You can use things like CASE and IF() to make
interesting, complex expressions. Read more about these functions here:
http://dev.mysql.com/doc/refman/5.0/...functions.html

> The MsSQL manual doesn't seem to cover this, nor does the two beginner
> books I have. A pointer to a reference where I can learn about advanced
> "Order by" would be great


It's not much, but here's an article I found through Google:
http://www.sqlteam.com/item.asp?ItemID=2209
It's written for Microsoft SQL Server users, but it mostly applies to MySQL
as well.

Regards,
Bill K.


Reply With Quote
  #3 (permalink)  
Old 03-25-2006
Marty Meyers
 
Posts: n/a
Default Re: Point me to some references for Sorting

Thanks Bill,
That second article may just do the trick. I did solve the problem one way
with a "Hammer". I added another column to the table, ran my query, did the
PHP analysis, wrote the results back to the table and the re-queried with
the new column. Works, but I'm not particularly proud of that solution.
Marty


"Bill Karwin" <bill@karwin.com> wrote in message
news:dvsr5k02je6@enews2.newsguy.com...
> "Marty Meyers" <martym@execpc.com> wrote in message
> news:1221g89ld0dtoab@corp.supernews.com...
> > if (ColA=0)
> > { // use Col B}
> >
> > elseif(ColA > 0 && colA < ColB)
> > { // use Col A
> > }
> > else
> > { //use ColB}

>
> SELECT ...
> ORDER BY CASE
> WHEN ColA = 0 THEN ColB
> WHEN ColA > 0 AND ColA < ColB THEN ColA
> ELSE ColB
> END
>
> It's based on the fact that the arguments to ORDER BY are expressions, not
> simply column names. You can use things like CASE and IF() to make
> interesting, complex expressions. Read more about these functions here:
> http://dev.mysql.com/doc/refman/5.0/...functions.html
>
> > The MsSQL manual doesn't seem to cover this, nor does the two beginner
> > books I have. A pointer to a reference where I can learn about

advanced
> > "Order by" would be great

>
> It's not much, but here's an article I found through Google:
> http://www.sqlteam.com/item.asp?ItemID=2209
> It's written for Microsoft SQL Server users, but it mostly applies to

MySQL
> as well.
>
> Regards,
> Bill K.
>
>



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:31 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0