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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
"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. |
|
|||
|
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. > > |